Views
2 weeks ago

tornadofx-guide

5. Data Controls If you

5. Data Controls If you do not care about exposing the Property in a function (which is common in practial usage) you can express your class like this: class Person(id: Int, name: String, birthday: LocalDate) { val idProperty = SimpleIntegerProperty(id) var id by idProperty val nameProperty = SimpleStringProperty(name) var name by nameProperty val birthdayProperty = SimpleObjectProperty(birthday) var birthday by birthdayProperty } val age: Int get() = Period.between(birthday, LocalDate.now()).years This alternative pattern exposes the Property as a field member instead of a function. If you like the above syntax but want to keep the function, you can make the property private and add the function like this: private val nameProperty = SimpleStringProperty(name) fun nameProperty() = nameProperty var name by nameProperty Choosing from these patterns are all a matter of taste, and you can use whatever version meets your needs or preferences best. You can also convert plain properties to JavaFX properties using the TornadoFX Plugin. Refer to Chapter 13 to learn how to do this. Using cellFormat() There are other extension functions applied to TableView that can assist the flow of declaring a TableView . For instance, you can call a cellFormat() function on a given column to apply formatting rules, such as highlighting "Age" values less than 18 (Figure 5.4). 56

5. Data Controls tableview(persons) { column("ID", Person::id) column("Name", Person::name) column("Birthday", Person::birthday) column("Age", Person::age).cellFormat { text = it.toString() style { if (it < 18) { backgroundColor += c("#8b0000") textFill = Color.WHITE } else { backgroundColor += Color.WHITE textFill = Color.BLACK } } } } Figure 5.4 Accessing Nested Properties Let's assume our Person object has a parent property which is also of of type Person . To create a column for the parent name, we have several options. Our first attempt is simply extracting the name property manually: column("Parent name", { it.value.parentProperty.value.nameProperty }) 57

GUIDE
GUIDE
GUIDE
Guide
Guide
GUIDE
guide
guide
Guide
Guide
GUIDE
GUIDE
GUIDE
Guide
Guide
Guide
GUIDE TO
GUIDE
GUIDE
GUIDE