Adding business logic to FXML

Although I strongly advise against doing it, you can put bits of the business logic directly into FXML by using the JavaScript engine and the fx:script tag.

In the next example, we declare a handler directly in the FXML and assign it to the button:

<?language javascript?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?><HBox alignment="CENTER" spacing="5.0" xmlns:fx="http://javafx.com/fxml/1">    <fx:script>        function handleButtonAction(event) {           textField.setText("clicked");        }    </fx:script>    <children>      <Label text="Label" />      <TextField fx:id="textField" />      <Button fx:id="button" text="Button"                     onAction="handleButtonAction(event);"/>   </children></HBox>

And, on button click, the TextField value will ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.