Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zilti
Last active May 16, 2019 02:18
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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*
[f]
(javafx.application.Platform/runLater f))
(defmacro run-later
[& body]
`(run-later* (fn [] ~@body)))
(defn run-now*
[f]
(let [result (promise)]
(run-later
(deliver result (try (f) (catch Throwable e e))))
@result))
(defmacro run-now
[& body]
`(run-now* (fn [] ~@body)))
(defn event-handler*
[f]
(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!")))
build)])
build))
build))
build)))
(run-now (.show @stage))
(defproject clj-javafx "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[com.oracle/javafx-runtime "2.2.0"]])
Sample project for the "Getting started with JavaFX in Clojure" ( https://coderwall.com/p/4yjy1a ) ProTip.
@jzwolak
Copy link

jzwolak commented Dec 3, 2014

I don't think this will work with the latest leiningen, clojure, and JavaFX. I had to change the last line of core.clj to:

(defn -main
  [& args]
  (run-now (.showAndWait @stage)))

And yes, it had to be in the -main function as well as using showAndWait instead of show.

@dsbw
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.

@4mitch
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