Views
5 months ago

tornadofx-guide

5. Data Controls val

5. Data Controls val persons = listOf( Person("Mary Hanes", "IT Administration", "mary.hanes@contoso.com", listOf( Person("Jacob Mays", "IT Help Desk", "jacob.mays@contoso.com"), Person("John Ramsy", "IT Help Desk", "john.ramsy@contoso.com"))), Person("Erin James", "Human Resources", "erin.james@contoso.com", listOf( Person("Erlick Foyes", "Customer Service", "erlick.foyes@contoso.com"), Person("Steve Folley", "Customer Service", "steve.folley@contoso.com"), Person("Larry Cable", "Customer Service", "larry.cable@contoso.com"))) ).observable() You can create a TreeTableView by merging the components needed for a TableView and TreeView together. You will need to call the populate() function as well as set the root TreeItem . val treeTableView = TreeTableView().apply { column("Name", Person::nameProperty) column("Department", Person::departmentProperty) column("Email", Person::emailProperty) /// Create the root item that holds all top level employees root = TreeItem(Person("Employees by leader", "", "", persons)) // Always return employees under the current person populate { it.value.employees } // Expand the two first levels root.isExpanded = true root.children.forEach { it.isExpanded = true } } // Resize to display all elements on the first two levels resizeColumnsToFitContent() It is also possible to work with more of an ad hoc backing store like a Map . That would look something like this: 66

5. Data Controls val tableData = mapOf( "Fruit" to arrayOf("apple", "pear", "Banana"), "Veggies" to arrayOf("beans", "cauliflower", "cale"), "Meat" to arrayOf("poultry", "pork", "beef") ) treetableview(TreeItem("Items")) { column("Type", { it.value.valueProperty() }) populate { if (it.value == "Items") tableData.keys else tableData[it.value]?.asList() } } DataGrid A DataGrid is similar to the GridPane in that it displays items in a flexible grid of rows and columns, but the similarities ends there. While the GridPane requires you to add Nodes to the children list, the DataGrid is data driven in the same way as TableView and ListView . You supply it with a list of items and tell it how to convert those children to a graphical representation. It supports selection of either a single item or multiple items at a time so it can be used as for example the display of an image viewer or other components where you want a visual representation of the underlying data. Usage wise it is close to a ListView , but you can create an arbitrary scene graph inside each cell so it is easy to visualize multiple properties for each item. val kittens = listOf("http://i.imgur.com/DuFZ6PQb.jpg", "http://i.imgur.com/o2QoeNnb.j pg") // more items here datagrid(kittens) { cellCache { imageview(it) } } Figure 5.8 67

GUIDE
GUIDE
Guide
Guide
GUIDE
GUIDE
GUIDE
GUIDE
guide
GUIDE
Guide
GUIDE
Guide