Cljfx
Declarative, functional and extensible wrapper of JavaFX inspired by better parts of react and re-frame
Install / Use
/learn @cljfx/CljfxREADME

Cljfx is a declarative, functional and extensible wrapper of JavaFX inspired by better parts of react and re-frame.
Rationale
I wanted to have an elegant, declarative and composable UI library for JVM and couldn't find one. Cljfx is inspired by react, reagent, re-frame and fn-fx.
Like react, it allows to specify only desired layout, and handles all actual changes underneath. Unlike react (and web in general) it does not impose xml-like structure of everything possibly having multiple children, thus it uses maps instead of hiccup for describing layout.
Like reagent, it allows to specify component descriptions using simple constructs such as data and functions. Unlike reagent, it rejects using multiple stateful reactive atoms for state and instead prefers composing ui in more pure manner.
Like re-frame, it provides an approach to building large applications using subscriptions and events to separate view from logic. Unlike re-frame, it has no hard-coded global state, and subscriptions work on referentially transparent values instead of ever-changing atoms.
Like fn-fx, it wraps underlying JavaFX library so developer can describe everything with clojure data. Unlike fn-fx, it is more dynamic, allowing users to use maps and functions instead of macros and deftypes, and has more explicit and extensible lifecycle for components.
Installation and requirements
Cljfx uses tools.deps, so you can add this repo with latest sha as a
dependency:
cljfx {:git/url "https://github.com/cljfx/cljfx" :sha "<insert-sha-here>"}
Cljfx is also published on Clojars, so you can add cljfx as a maven
dependency, current version is on this badge:
Minimum required version of clojure is 1.10.
When depending on git coordinates, minimum required Java version is 11. When using maven dependency, both Java 8 (assumes it has JavaFX provided in JRE) and Java 11 (via openjfx dependency) are supported. You don't need to configure anything in this regard: correct classifiers are picked up automatically.
Please note that JavaFX 8 is outdated and has problems some people consider severe: it does not support HiDPI scaling on Linux, and sometimes crashes JVM on macOS Mojave. You should prefer JDK 11.
Overview
Hello world
Components in cljfx are described by maps with :fx/type key. By
default, fx-type can be:
- a keyword corresponding to some JavaFX class
- a function, which receives this map as argument and returns another description
- an implementation of Lifecycle protocol (more on that in extending cljfx section)
Minimal example:
(ns example
(:require [cljfx.api :as fx]))
(fx/on-fx-thread
(fx/create-component
{:fx/type :stage
:showing true
:title "Cljfx example"
:width 300
:height 100
:scene {:fx/type :scene
:root {:fx/type :v-box
:alignment :center
:children [{:fx/type :label
:text "Hello world"}]}}}))
Evaluating this code will create and show a window:

The overall mental model of these descriptions is this:
- whenever you need a JavaFX class, use map where
:fx/typekey has a value of a kebab-cased keyword derived from that class name - other keys in this map represent JavaFX properties of that class (also in kebab-case);
- if prop x can be changed by user, there is a corresponding
:on-x-changedprop for observing these changes
Renderer
To be truly useful, there should be some state and changes over time, for this matter there is a renderer abstraction, which is a function that you may call whenever you want with new description, and cljfx will advance all the mutable state underneath to match this description. Example:
(def renderer
(fx/create-renderer))
(defn root [{:keys [showing]}]
{:fx/type :stage
:showing showing
:scene {:fx/type :scene
:root {:fx/type :v-box
:padding 50
:children [{:fx/type :button
:text "close"
:on-action (fn [_]
(renderer {:fx/type root
:showing false}))}]}}})
(renderer {:fx/type root
:showing true})
Evaluating this code will show this:

Clicking close button will hide this window.
Renderer batches descriptions and re-renders views on fx thread only with last received description, so it is safe to call many times at once. Calls to renderer function return derefable that will contain component value with most recent description.
Atoms
Example above works, but it's not very convenient: what we'd really like is to have a single global state as a value in an atom, derive our description of JavaFX state from this value, and change this atom's contents instead. Here is how it's done:
;; Define application state
(def *state
(atom {:title "App title"}))
;; Define render functions
(defn title-input [{:keys [title]}]
{:fx/type :text-field
:on-text-changed #(swap! *state assoc :title %)
:text title})
(defn root [{:keys [title]}]
{:fx/type :stage
:showing true
:title title
:scene {:fx/type :scene
:root {:fx/type :v-box
:children [{:fx/type :label
:text "Window title input"}
{:fx/type title-input
:title title}]}}})
;; Create renderer with middleware that maps incoming data - description -
;; to component description that can be used to render JavaFX state.
;; Here description is just passed as an argument to function component.
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc assoc :fx/type root)))
;; Convenient way to add watch to an atom + immediately render app
(fx/mount-renderer *state renderer)
Evaluating code above pops up this window:

Editing input then immediately updates displayed app title.
Map events
Consider this example:
(defn todo-view [{:keys [text id done]}]
{:fx/type :h-box
:children [{:fx/type :check-box
:selected done
:on-selected-changed #(swap! *state assoc-in [:by-id id :done] %)}
{:fx/type :label
:style {:-fx-text-fill (if done :grey :black)}
:text text}]})
There are problems with using functions as event handlers:
- Performing mutation from these handlers requires coupling with that
state, thus making
todo-viewdependent on mutable*state - Updating state from listeners complects logic with view, making application messier over time
- There are unnecessary reassignments to
on-selected-changed: functions have no equality semantics other than their identity, so on every change to this view (for example, when changing it's text),on-selected-changedwill be replaced with another function with same behavior.
To mitigate these problems, cljfx allows to define event handlers as
arbitrary maps, and provide a function to a renderer that performs
actual handling of these map-events (with additional :fx/event key
containing dispatched event):
;; Define view as just data
(defn todo-view [{:keys [text id done]}]
{:fx/type :h-box
:spacing 5
:padding 5
:children [{:fx/type :check-box
:selected done
:on-selected-changed {:event/type ::set-done :id id}}
{:fx/type :label
:style {:-fx-text-fill (if done :grey :black)}
:text text}]})
;; Define single map-event-handler that does mutation
(defn map-event-handler [event]
(case (:event/type event)
::set-done (swap! *state assoc-in [:by-id (:id event) :done] (:fx/event event))))
;; Provide map-event-handler to renderer as an option
(fx/mount-renderer
*state
(fx/create-renderer
:middleware (fx/wrap-map-desc assoc :fx/type root)
:opts {:fx.opt/map-event-handler map-event-handler}))
You can see full example at examples/e09_todo_app.clj.
Interactive development
Another useful aspect of renderer function that should be used during development is refresh functionality: you can call renderer function with zero args and it will recreate all the components with current description.
See walk-through in examples/e12_interactive_development.clj as an example of how to iterate on cljfx app in REPL.
Dev tools
Check out cljfx/dev for tools that might help you when developing cljfx applications. These tools include:
- specs and validation, both for individual cljfx descriptions and running apps;
- helper reference for existing types and their props;
- cljfx component stack reporting in exceptions to help with debugging.
Styling
Iterating on styling is usually cumbersome: styles are defined in external files, they are not reloaded on change, they are opaque: you can't refer from the code to values defined in CSS. Cljfx has a complementary library that aims to help with all those problems: cljfx/css.
Special keys
Sometimes components accept specially treated keys. Main uses are:
- Reordering of nodes (instead of re-creating them) in parents that may
have many children. Descriptions that have
:fx/keyduring adva
