Views
6 months ago

tornadofx-guide

5. Data Controls Data

5. Data Controls Data Controls Any significant application works with data, and providing a means for users to view, manipulate, and modify data is not a trivial task for user interface development. Fortunately, TornadoFX streamlines many JavaFX data controls such as ListView , TableView , TreeView , and TreeTableView . These controls can be cumbersome to set up in a purely object-oriented way. But using builders through functional declarations, we can code all these controls in a much more streamlined way. ListView A ListView is similar to a ComboBox but it displays all items within a ScrollView and has the option of allowing multiple selections, as shown in Figure 5.1 listview { items.add("Alpha") items.add("Beta") items.add("Gamma") items.add("Delta") items.add("Epsilon") selectionModel.selectionMode = SelectionMode.MULTIPLE } Figure 5.1 You can also provide it an ObservableList of items up front and omit the type declaration since it can be inferred. Using an ObservableList has the benefit that changes to the list will automatically be reflected in the ListView . 52

5. Data Controls val greekLetters = listOf("Alpha","Beta", "Gamma","Delta","Epsilon").observable() listview(greekLetters) { } selectionModel.selectionMode = SelectionMode.MULTIPLE Like most data controls, keep in mind that by default the ListView will call toString() to render the text for each item in your domain class. To render anything else, you will need to create your own custom cell formatting. To read about custom cell formatting and nodes for a ListView , read Appendix A3 - Custom Cell Formatting in ListView TableView Probably one of the most significant builders in TornadoFX is the one for TableView . If you have worked with JavaFX, you might have experienced building a TableView in an objectoriented way. But TornadoFX provides a functional declaration construct pattern using extension functions that greatly simplify the coding of a TableView . Say you have a domain type, such as Person . class Person(val id: Int, val name: String, val birthday: LocalDate) { } val age: Int get() = Period.between(birthday, LocalDate.now()).years Take several instances of Person and put them in an ObservableList . private val persons = listOf( Person(1,"Samantha Stuart",LocalDate.of(1981,12,4)), Person(2,"Tom Marks",LocalDate.of(2001,1,23)), Person(3,"Stuart Gills",LocalDate.of(1989,5,23)), Person(3,"Nicole Williams",LocalDate.of(1998,8,11)) ).observable() You can quickly declare a TableView with all of its columns using a functional construct, and specify the items property to an ObservableList (Figure 5.3). 53

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