cljfx

https://github.com/cljfx/cljfx
jlmr 2020-02-09T12:45:02.043700Z

Hi, how can I focus a textfield once it’s created? I’ve tried:

{:fx/type fx/ext-on-instance-lifecycle
 :on-created #(.requestFocus %)
 :desc {:fx/type :text-field}

vlaaad 2020-02-09T12:47:16.043800Z

Almost :) I had this problem as well. requestFocus does nothing then node is not on a scene, and it will be added to scene later

vlaaad 2020-02-09T12:49:24.044100Z

@jlmr I have this thing:

(defn focus-when-on-scene! [^Node node]
  (if (some? (.getScene node))
    (.requestFocus node)
    (.addListener (.sceneProperty node)
                  (reify ChangeListener
                    (changed [this _ _ new-scene]
                      (when (some? new-scene)
                        (.removeListener (.sceneProperty node) this)
                        (fx/run-later
                          (.requestFocus node))))))))

(defn ext-focused-by-default [{:keys [desc]}]
  {:fx/type fx/ext-on-instance-lifecycle
   :on-created focus-when-on-scene!
   :desc desc})

vlaaad 2020-02-09T12:49:45.044600Z

Don't remember why it's in run-later , might be unnecessary

jlmr 2020-02-09T12:56:13.045100Z

Works great, also without run-later thanks again for all your help!

vlaaad 2020-02-09T13:03:45.045200Z

You are welcome 😊