Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example boot-ified Swing app that contains a JavaFX WebView (Webkit instance).
#!/usr/bin/env boot
;; -*- mode: Clojure;-*-
(set-env! :dependencies '[[seesaw "1.4.5"]])
(use 'seesaw.core)
(import '(javafx.scene.web WebView)
'(javafx.scene SceneBuilder)
'(javafx.scene.layout VBoxBuilder))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; need to wrap calls that happen in the UI thread
(defn run-later*
[f]
(javafx.application.Platform/runLater f))
(defmacro run-later
[& body]
`(boot.user/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]
`(boot.user/run-now* (fn [] ~@body)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; a couple of helpers for the webview
(defonce web-view-panel (javafx.embed.swing.JFXPanel.))
(def web-view (atom nil))
(def engine (atom nil))
(defn set-html! [html]
(run-later (.loadContent @engine html)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -main [& args]
(native!) ;; native look & feel, please
;; set up the webview in a JavaFX "scene"
(run-now
(reset! web-view (doto (WebView.) (.setPrefHeight 900)))
(reset! engine (.getEngine @web-view))
(.setScene web-view-panel
(.. SceneBuilder create
(height 980) (width 640)
(root (.. VBoxBuilder create
(minHeight 980) (minWidth 640)
(children [@web-view])
build))
build)))
;; put the web-view in a Swing window frame
(-> (frame :title "Füchse"
:content web-view-panel
:on-close :exit)
pack!
show!)
;; deliver an important message
(set-html! "<html><body><strong style='font-size:36px;'>Füchse sind gar keine Rudeltiere</strong></body></html>")
;; when run in a boot script, the main thread exits and takes the UI
;; thread with it (?!?), so I added this as a gross hack to prevent
;; that
(println (let [p (promise)] @p)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.