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.

4. Basic Controls<br />

If you need to save references to controls such as the TextFields, you can save them to<br />

variables or properties since the functions return the produced controls. Until we learn more<br />

robust modeling techniques, it is recommended you use the singleAssign() delegates to<br />

ensure the properties are only assigned once.<br />

import javafx.scene.control.TextField<br />

import <strong>tornadofx</strong>.*<br />

class MyView : View() {<br />

var firstNameField: TextField by singleAssign()<br />

var lastNameField: TextField by singleAssign()<br />

}<br />

override val root = vbox {<br />

hbox {<br />

label("First Name")<br />

firstNameField = textfield()<br />

}<br />

hbox {<br />

label("Last Name")<br />

lastNameField = textfield()<br />

}<br />

button("LOGIN") {<br />

useMaxWidth = true<br />

action {<br />

println("Logging in as ${firstNameField.text} ${lastNameField.text}")<br />

}<br />

}<br />

}<br />

Note that non-builder extension functions and properties have been added to different<br />

controls as well. The useMaxWidth is an extended property for Node , and it sets the Node<br />

to occupy the maximum width allowed. We will see more of these helpful extensions<br />

throughout the next few chapters. We will also see each corresponding builder for each<br />

JavaFX control. With the concepts understood above, you can read about these next<br />

chapters start to finish or as a reference.<br />

Builders for Basic Controls<br />

The rest of this chapter will cover builders for common JavaFX controls like Button ,<br />

Label , and TextField . The next chapter will cover builders for data-driven controls like<br />

ListView , TableView , and TreeTableView .<br />

Button<br />

35

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

Saved successfully!

Ooh no, something went wrong!