JavaFX FXML #609
vsquared
started this conversation in
Show and tell
JavaFX FXML
#609
Replies: 1 comment 1 reply
-
Have you tried |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The following source code demonstrates using JavaFX's FXML in py5 which works differently than it does in Processing's Java mode. Java FXML examples usually show using a 'Controller' class for event handling. However, in py5 this is not possible because setController() requires a java.lang.Object which py5 cannot deliver. To get around this limitation an alternative technique shown below has been found; so far I have not seen it described elsewhere. It works by gleaning the root control (a Pane in our case) from the output of loader.load() and then requesting a control list by using pane.getChildren(). Controls on the list are then accessible by using their list index, eg ctrl[index]. Event handling is achieved by using ctrl[x].setOnAction() to call a free-standing function which normally would be part of the Controller class, eg def btnAction(event). The 'event' parameter is important and the function will not work correctly without it. This demo uses a standard .xml file which may be edited in a text editor of your choice. A .fxml file created by the app SceneBuilder may also be used (files edited in Android Studio). Either file type will work. A controller or onAction() should not be set in either file to insure that the file will load correctly. The object of using JavaFX FXML is to isolate gui code from the main app code thereby simplifying it.
ctrl2.xml file (could also use .fxml)
Output:

Beta Was this translation helpful? Give feedback.
All reactions