Views
3 months ago

tornadofx-guide

Advanced Data Controls

Advanced Data Controls Figure 5.2 - A custom cell rendering for ListView The cellFormat function lets you configure the text and/or graphic property of the cell whenever it comes into view on the screen. The cells themselves are reused, but whenever the ListView asks the cell to update its content, the cellFormat function is called. In our example we only assign to graphic , but if you just want to change the string representation you should assign it to text . It is completely legitimate to assign it to both text and graphic . The values will automatically be cleared by the cellFormat function when a certain list cell is not showing an active item. Note that assigning new nodes to the graphic property every time the list cell is asked to update can be expensive. It might be fine for many use cases, but for heavy node graphs, or node graphs where you utilize binding towards the UI components inside the cell, you should cache the resulting node so the Node graph will only be created once per node. This is done using the cache wrapper in the above example. 202

Advanced Data Controls Assign If Null If you have a reason for wanting to recreate the graphic property for a list cell, you can use the assignIfNull helper, which will assign a value to any given property if the property doesn't already contain a value. This will make sure that you avoid creating new nodes if updateItem is called on a cell that already has a graphic property assigned. cellFormat { graphicProperty().assignIfNull { label("Hello") } } ListCellFragment The ListCellFragment is a special fragment which can help you manage ListView cells. It extends Fragment , and includes some extra ListView specific fields and helpers. You never instantiate these fragments manually, instead you instruct the ListView to create them as needed. There is a one-to-one correlation between ListCell and ListCellFragment instances. Only one ListCellFragment instance will over its lifecycle be used to represent different items. To understand how this works, let's consider a manually implemented ListCell , essentially the way you would do in vanilla JavaFX. The updateItem function will be called when the ListCell should represent a new item, no item, or just an update to the same item. When you use a ListCellFragment , you do not need to implement something akin to updateItem , but the itemProperty inside it will update to represent the new item automatically. You can listen to changes to the itemProperty , or better yet, bind it directly to a ViewModel . That way your UI can bind directly to the ViewModel and no longer need to care about changes to the underlying item. Let's recreate the form from the cellFormat example using a ListCellFragment . We need a ViewModel which we will call PersonModel (Please see the Editing Models and Validation chapter for a full explanation of the ViewModel ) For now, just imagine that the ViewModel acts as a proxy for an underlying Person , and that the Person can be changed while the observable values in the ViewModel remain the same. When we have created our PersonCellFragment , we need to configure the ListView to use it: listview(personlist) { } cellFragment(PersonCellFragment::class) 203

guide
A guide
Guide
Guide
Guide
guide
GUIDE
GUIDE
Guide
A FALCON GUIDE® - Falcon Guides
visitor guide visitor guide visitor guide visitor guide visitor guide ...