Views
8 months ago

tornadofx-guide

10. FXML FXML file

10. FXML FXML file locations By default, build tools like Maven and Gradle will ignore any extra resources you put into your source root folders, so if you put your FXML files there they won't be available at runtime unless you specifically tell your build tool to include them. This could still be problematic because IDEA might not pick up your custom resource location from the build file, once again resulting in failure at runtime. For that resource, we recommend that you place your FXML files in src/main/resources and either follow the same folder structure as your packages, or put them all in a views folder or similar. The latter requires you to add the FXML location parameter to the fxml delegate, and might be messy if you have a large number of Views, so going with the default is a good idea. Accessing Nodes with the fxid delegate Using just FXML, we have wired the Button to call increment() every time it is called. We still need to bind the counter value to the text property of the Label . To do this, we need an identifier for the Label , so in our FXML file we add the fx:id attribute to it. Now we can inject this Label into our View class: val counterLabel : Label by fxid() This tells TornadoFX to look for a Node in our structure with the fx:id property with the same name as the property we defined (which is "counterLabel"). It is also possible to use another property name in the View and add a name parameter to the fxid delegate: val myLabel : Label by fxid("counterLabel") Now that we have a hold of the Label , we can use the binding shortcuts of TornadoFX to bind the counter value to the text property of the counterLabel . Our whole View should now look like this: 148

10. FXML class CounterView : View() { override val root : BorderPane by fxml() val counter = SimpleIntegerProperty() val counterLabel: Label by fxid() init { } counterLabel.bind(counter) } fun increment() { } counter.value += 1 Our app is now complete. Every time the button is clicked, the label will increment its count. Internationalization JavaFX has strong support for multi-language UI's. To support internationalization in FXML, you normally have to register a resource bundle with the FXMLLoader and it will in return replace instances of resource names with their locale-specific value. A resource name is the key in the resource bundle prepended with % . TornadoFX makes this easier by supporting a convention for resource bundles: Create a resource bundle with the same base name as your View , and it will be automatically loaded, both for use programatically within the View and from the FXML file. Let's internationalize the button text in our UI. Create a file called CounterView.properties and add the following content: clickToIncrement=Click to increment If you want to support multiple languages, create a file with the same base name followed by an underscore, and then the language code. For instance, to support French create the file CounterView_fr.properties . The closest geographical match to the current locale will be used. clickToIncrement=Cliquez sur incrément Now we swap the button text with the resource key in the FXML file. 149

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