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.

5. Data Controls<br />

val greekLetters = listOf("Alpha","Beta",<br />

"Gamma","Delta","Epsilon").observable()<br />

listview(greekLetters) {<br />

}<br />

selectionModel.selectionMode = SelectionMode.MULTIPLE<br />

Like most data controls, keep in mind that by default the ListView will call toString() to<br />

render the text for each item in your domain class. To render anything else, you will need to<br />

create your own custom cell formatting.<br />

To read about custom cell formatting and nodes for a ListView , read Appendix A3 -<br />

Custom Cell Formatting in ListView<br />

TableView<br />

Probably one of the most significant builders in TornadoFX is the one for TableView . If you<br />

have worked with JavaFX, you might have experienced building a TableView in an objectoriented<br />

way. But TornadoFX provides a functional declaration construct pattern using<br />

extension functions that greatly simplify the coding of a TableView .<br />

Say you have a domain type, such as Person .<br />

class Person(val id: Int, val name: String, val birthday: LocalDate) {<br />

}<br />

val age: Int get() = Period.between(birthday, LocalDate.now()).years<br />

Take several instances of Person and put them in an ObservableList .<br />

private val persons = listOf(<br />

Person(1,"Samantha Stuart",LocalDate.of(1981,12,4)),<br />

Person(2,"Tom Marks",LocalDate.of(2001,1,23)),<br />

Person(3,"Stuart Gills",LocalDate.of(1989,5,23)),<br />

Person(3,"Nicole Williams",LocalDate.of(1998,8,11))<br />

).observable()<br />

You can quickly declare a TableView with all of its columns using a functional construct, and<br />

specify the items property to an ObservableList (Figure 5.3).<br />

53

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

Saved successfully!

Ooh no, something went wrong!