Views
2 months ago

tornadofx-guide

10. FXML

10. FXML You may notice above you have to import the types you use in FXML just like coding in Java or Kotlin. Intellij IDEA should have a plugin to support using ALT+ENTER to generate the import statements. If you load this file in Scene Builder you will see the following result (Figure 9.1). Figure 9.1 Next let's load this FXML into TornadoFX. Loading FXML into TornadoFX 146

10. FXML We have created an FXML file containing our UI structure, but now we need to load it into a TornadoFX View for it to be usable. Logically, we can load this Node hierarchy into the root node of our View . Define the following View class: class CounterView : View() { } override val root : BorderPane by fxml() Note that the root property is defined by the fxml() delegate. The fxml() delegate takes care of loading the corresponding CounterView.fxml into the root property. If we placed CounterView.fxml in a different location (such as /views/ ) that is different than where the CounterView file resides, we would add a parameter. class CounterView : View() { } override val root : BorderPane by fxml("/views/CounterView.fxml") We have laid out the UI, but it has no functionality yet. We need to define a variable that holds the number of times the Button has been clicked. Add a variable called counter and define a function that will increment its value: class CounterView : View() { override val root : BorderPane by fxml() val counter = SimpleIntegerProperty() } fun increment() { } counter.value += 1 We want the increment() function to be called whenever the Button is clicked. Back in the FXML file, add the onAction attribute to the button: Since the FXML file automatically gets bound to our View, we can reference functions via the #functionName syntax. Note that we do not add parenthesis to the function call, and you cannot pass parameters directly. You can however add a parameter of type javafx.event.ActionEvent to the increment function if you want inspect the source Node of the action or check what kind of action triggered the button. For this example we do not need it, so we leave the increment function without parameters. 147

Guide
GUIDE
Guide
GUIDE
Guide
Guide
GUIDE
Guide
GUIDE
Guide
GUIDE
GUIDE