Skip to content

Instantly share code, notes, and snippets.

Last active May 16, 2019 02:18
Show Gist options
  • Save zilti/6286307 to your computer and use it in GitHub Desktop.
Save zilti/6286307 to your computer and use it in GitHub Desktop.
(ns jfxtest.core
(:import (javafx.scene SceneBuilder)
(javafx.scene.control ButtonBuilder)
(javafx.scene.layout VBoxBuilder)
(javafx.stage StageBuilder)))
(defonce force-toolkit-init (javafx.embed.swing.JFXPanel.))
(defn run-later*
(javafx.application.Platform/runLater f))
(defmacro run-later
[& body]
`(run-later* (fn [] ~@body)))
(defn run-now*
(let [result (promise)]
(deliver result (try (f) (catch Throwable e e))))
(defmacro run-now
[& body]
`(run-now* (fn [] ~@body)))
(defn event-handler*
(reify javafx.event.EventHandler
(handle [this e] (f e))))
(defmacro event-handler [arg & body]
`(event-handler* (fn ~arg ~@body)))
(def stage (atom nil))
;; You of course don't have to write it all in one block. Using a (def button (.. ButtonBuilder ...)) and then adding button is just as good (probably better most of the times).
(run-now (reset! stage (.. StageBuilder create
(title "Hello JavaFX")
(scene (.. SceneBuilder create
(height 480) (width 640)
(root (.. VBoxBuilder create
(minHeight 480) (minWidth 640)
(children [(.. ButtonBuilder create
(text "Say \"Hello Clojure\"!")
(onAction (event-handler [_] (println "Hello Clojure!")))
(run-now (.show @stage))
(defproject clj-javafx "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url ""
:license {:name "Eclipse Public License"
:url ""}
:dependencies [[org.clojure/clojure "1.5.1"]
[ "2.2.0"]])
Sample project for the "Getting started with JavaFX in Clojure" ( ) ProTip.
Copy link

dsbw commented Jan 30, 2019

It worked well for me. I just pasted it into my core and ran "lein repl" and it popped up.

"lein run" also worked.

Copy link

4mitch commented Apr 27, 2019

Window has popped up indeed. But REPL shows Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: create in this context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment