Views
9 months ago

tornadofx-guide

5. Data Controls

5. Data Controls tableview(persons) { column("ID",Person::id) column("Name", Person::name) column("Birthday", Person::birthday) column("Age",Person::age) } Figure 5.3 The column() functions are extension functions for TableView accepting a header name and a mapped property using reflection syntax. TornadoFX will then take each mapping to render a value for each cell in that given column. If you want granular control over TableView column resize policies, see Appendix A2 for more information on SmartResize policies. Using "Property" properties If you follow the JavaFX Property conventions to set up your domain class, it will automatically support value editing. You can create these Property objects the conventional way, or you can use TornadoFX's property delegates to automatically create these Property declarations as shown below. 54

5. Data Controls class Person(id: Int, name: String, birthday: LocalDate) { var id by property(id) fun idProperty() = getProperty(Person::id) var name by property(name) fun nameProperty() = getProperty(Person::name) var birthday by property(birthday) fun birthdayProperty() = getProperty(Person::birthday) } val age: Int get() = Period.between(birthday, LocalDate.now()).years You need to create xxxProperty() functions for each property to support JavaFX's naming convention when it uses reflection. This can easily be done by relaying their calls to getProperty() to retrieve the Property for a given field. See Appendix A1 for detailed information on how these property delegates work. Now on the TableView , you can make it editable, map to the properties, and apply the appropriate cell-editing factories to make the values editable. override val root = tableview(persons) { isEditable = true column("ID",Person::idProperty).makeEditable() column("Name", Person::nameProperty).makeEditable() column("Birthday", Person::birthdayProperty).makeEditable() column("Age",Person::age) } To allow editing and rendering, TornadoFX provides a few default cell factories you can invoke on a column easily through extension functions. Extension Function useTextField() useComboBox() useChoiceBox() useCheckBox() useProgressBar() Description Uses a standard TextField to edit values with a provided StringConverter Edits a cell value via a ComboBox with a specified ObservableList of applicable values Accepts value changes to a cell with a ChoiceBox Renders an editable CheckBox for a Boolean value column Renders the cell as a ProgressBar for a Double value column Property Syntax Alternatives 55

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