Javafx 2 javascript api - getting java controller

I have an FXML file that has many controller methods that I send to a regular controller. However, I recently added some small handlers that did very trivial things, and I was wondering if I could do them in javascript in the FXML file itself. However, it looks like I ran into a problem: I cannot find a documented way to get the controller object in javascript. How can I get the controller object in javascript? (and no, I cannot use static methods because I have many such controllers on many objects)

Current code using Java: FXML file:

... <GridPane fx:id="moveOverlay" onMouseClicked="#mouseOverlayClicked" ...> .... 

and java controller:

 ... @FXML private void mouseOverlayClicked(MouseEvent e) { if (e.getClickCount() > 1) { this.enableNestedEditing(); } else if (e.isControlDown()) { this.selectSelf(); } } ... 

Desired FXML:

 ... <fx:script> var controller = [what goes here?]; function mouseOverlayClicked(e) { if (e.getClickCount() > 1) { controller.enableNestedEditing(); else if (e.isControlDown()) { controller.selectSelf(); } </fx:script> ... <GridPane fx:id="moveOverlay" onMouseClicked="mouseOverlayClicked" ...> ... 

and no corresponding event handler in the java class (except for other methods)

+4
source share
1 answer

Easy enough, the controller already defined in the javascript fxml part. Therefore var controller = not required.

Example:

 <?xml version="1.0" encoding="utf-8"?> <?language javascript?> <?import javafx.scene.layout.VBox?> <?import javafx.scene.control.Button?> <VBox xmlns:fx="http://javafx.com/fxml" fx:controller="fxmltest.SomeController"> <fx:script> function buttonClicked() { controller.print("hallo"); } </fx:script> <children> <Button text="John Doe" onAction="buttonClicked()"/> </children> </VBox> 

Controller:

 package fxmltest; import javafx.fxml.FXML; public class SomeController { @FXML public void print(String s){ System.out.println("from javascript: "+s); } } 

Result:

enter image description here

+2
source

All Articles