In this post I will show how to customize the rendering of a JavaFX 2 TableView. The Birthday column in the screenshot below is a formatted
Calendar object. Depending on the year, the text color is changed.
How a Table Cell is Rendered
Cell Value Factory
The cell must know which part of
Person it needs to display. For all cells in the birthday column this will be the
This is our birthday column:
And later during initialization, we’ll set the
Cell Value Factory:
So far nothing too fancy.
Once the cell has the value, it must know how to display that value. In our case, the birthday’s
Calendar value must be formatted and colored depending on the year.
[update 2012-12-27: Set text to null if cell is empty. See comment by James_D below]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Cell Factory contains some complicated stuff (Callback, Generics and Anonymous Inner Classes). Don’t worry too much about all this. Just focus on the important part which is the
updateItem(...) method gets called whenever the cell should be rendered. We receive the
Calendar item that must be rendered. If empty is
true we don’t do anything. Otherwise we format the item and set the text of the cell. Depending on the year, we also set the text color.
ListView and TreeView
Download the complete tableview-cell-renderer example.