This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-18
Channels
- # announcements (2)
- # babashka (35)
- # beginners (59)
- # calva (17)
- # cider (3)
- # clerk (7)
- # clj-kondo (21)
- # cljfx (9)
- # cljs-dev (76)
- # clojure (98)
- # clojure-austin (3)
- # clojure-brasil (1)
- # clojure-europe (11)
- # clojure-gamedev (4)
- # clojurescript (14)
- # consulting (7)
- # cursive (6)
- # datascript (4)
- # datomic (12)
- # emacs (18)
- # events (2)
- # graalvm (9)
- # humbleui (3)
- # hyperfiddle (18)
- # jobs (4)
- # missionary (12)
- # nextjournal (2)
- # nrepl (7)
- # off-topic (31)
- # practicalli (2)
- # rdf (6)
- # releases (2)
- # scittle (10)
- # xtdb (9)
Hello guys! Do you have an example of how a app can show an alert, for confirming actions? I looked into the documentation, everything I have is this:
(defn alert [_]
{:fx/type :alert
:alert-type :warning
:showing true
:on-close-request (fn [^DialogEvent e]
(when (nil? (.getResult ^Dialog (.getSource e)))
(.consume e)))
:on-hidden (fn [_]
(reset! *state :choose-vault))
:header-text "Nuke launch inevitable"
:content-text "Please press Yes"
:button-types [:yes]})
But how is it called/added into, say:
(s/defn login
[]
{:fx/type :v-box
:padding 10
:spacing 10
:alignment :top-center
:children [{:fx/type :label
:style "-fx-font-size: 40px;"
:text "Login"}
{:fx/type :text-field
:prompt-text "E-mail"
:disable (get state/*state :login-waiting false)
:max-width 200
:on-text-changed #(swap! state/*state assoc :login-email %)
:on-key-pressed {:event/type ::press}}
{:fx/type :password-field
:prompt-text "Password"
:disable (get state/*state :login-waiting false)
:max-width 200
:on-text-changed #(swap! state/*state assoc :login-password %)
:on-key-pressed {:event/type ::press}}
{:fx/type :button
:text (if (get state/*state :login-waiting false) "Verifying..." "Submit")
:pref-width 200
:on-action controllers.tower/authenticate!}]})
Thanks!If you want it super simple, you can do it like that:
(= javafx.scene.control.ButtonType/YES
(.get
@(fx/on-fx-thread
(.showAndWait
(fx/instance
(fx/create-component
{:fx/type :alert
:alert-type :information
:content-text "Hello?"
:button-types [:no :yes]}))))))
(^ you can try it in the REPL)You don’t need fx/on-fx-thread
if you run this code on fx thread (e.g. in a on-action callback)
this code create a dialog outside of the main app cljfx app lifecycle, and blocks until user responds, which is what you want with dialogs
if you want the dialog to be a part of the whole app lifecycle, you can achieve this using fx/ext-many
lifecycle by defining many windows in your app, e.g. one main window with your app and extra dialog windows that appear or disappear as needed.
There is example here https://github.com/cljfx/cljfx/blob/master/examples/e21_extension_lifecycles.clj#L78-L103
there is an example of a button with confirmation dialog and how it’s managed inside an app here https://github.com/cljfx/cljfx/blob/master/examples/e22_button_with_confirmation_dialog.clj
Many thanks master! I'll give this try!