09.04.2018 Views

tornadofx-guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10. FXML<br />

class CounterView : View() {<br />

override val root : BorderPane by fxml()<br />

val counter = SimpleIntegerProperty()<br />

val counterLabel: Label by fxid()<br />

init {<br />

}<br />

counterLabel.bind(counter)<br />

}<br />

fun increment() {<br />

}<br />

counter.value += 1<br />

Our app is now complete. Every time the button is clicked, the label will increment its<br />

count.<br />

Internationalization<br />

JavaFX has strong support for multi-language UI's. To support internationalization in FXML,<br />

you normally have to register a resource bundle with the FXMLLoader and it will in return<br />

replace instances of resource names with their locale-specific value. A resource name is the<br />

key in the resource bundle prepended with % .<br />

TornadoFX makes this easier by supporting a convention for resource bundles: Create a<br />

resource bundle with the same base name as your View , and it will be automatically<br />

loaded, both for use programatically within the View and from the FXML file.<br />

Let's internationalize the button text in our UI. Create a file called CounterView.properties<br />

and add the following content:<br />

clickToIncrement=Click to increment<br />

If you want to support multiple languages, create a file with the same base name followed by<br />

an underscore, and then the language code. For instance, to support French create the file<br />

CounterView_fr.properties . The closest geographical match to the current locale will be<br />

used.<br />

clickToIncrement=Cliquez sur incrément<br />

Now we swap the button text with the resource key in the FXML file.<br />

149

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!