binding - Clojure and JavaFX 2.0 -- How to access element's ID from clojure callback function -
following javafx tutorial here: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm, trying make run in clojure. i'm doing lein run
after setting :aot :all
, stuff (:gen-class)
etc. took few days of figuring out, seems working.
in src/jfxtwo/clojureexamplecontroller.clj
:
(defn -handlesubmitbuttonaction [^actionevent event] (let [actiontarget (text.)] (println "event button pressed") (println "event instance:" event) (println "event class:" (class event)) (.settext actiontarget "sign in button pressed...")))
in resources/fxml_example.fxml
:
<gridpane fx:controller= "jfxtwo.clojureexamplecontroller" xmlns:fx= "http://javafx.com/fxml" alignment= "center" hgap= "10" vgap= "10" styleclass= "root" > ... <button text= "sign in" onaction= "#handlesubmitbuttonaction" /> ... <text fx:id= "actiontarget" gridpane.columnindex= "1" gridpane.rowindex= "6" /> ...
i have clojure code able read fxml , css file generate proper gui. when press button can see event handler being called, don't know how access text
want change, or actionevent
instance associated button press. tried (println event)
, (println (class event))
expecting see actionevent
instance, results in showing me whatever reason callback function thinks event clojureexamplecontroller
, though type hint says should actionevent
:
event button pressed event instance: #<clojureexamplecontroller jfxtwo.clojureexamplecontroller@3e61061d> event class: jfxtwo.clojureexamplecontroller
the java code looks this:
public class jfxappsamplecontroller { @fxml private text actiontarget; @fxml protected void handlesubmitbuttonaction(actionevent event) { actiontarget.settext("sign in button pressed"); } }
clearle @fxml
annotation doing magic here. what's going on, , how make work in clojure?
also, there way bind button press text change directly in fxml don't have handle gui->gui change in code-behind, , instead deal logic associated button press? i'm guessing yes, haven't gotten point in tutorial.
thanks!
you need explicit this
argument in handler:
(defn -handlesubmitbuttonaction [this ^actionevent event] ...)
perhaps more accurate way of putting event
is this
argument in handler exhibited in question text , need add second argument accept event in , move type hint (and rename first argument this
readability).
given fact code gets called @ all, seem javafx happy call handler without passing event @ if doesn't care (as evidenced not having formal parameter corresponding it).
the type hint's purpose allow clojure compiler avoid emitting reflective code when actionevent
methods called on event
. not prevent passing object of different type function.
Comments
Post a Comment