Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TableHeaderRow from TableView nowadays in JavaFX?

I saw examples on how to get table header in many places with code

TableHeaderRow header = (TableHeaderRow) tableView.lookup("TableHeaderRow");

like here: How to prevent TableView from doing TableColumn re-order in javaFX 8?

But this code returns null for me.

How to reach TableHeaderRow then?

like image 374
Dims Avatar asked Aug 02 '16 11:08

Dims


2 Answers

The TableHeaderRow is created by the Skin and the default Skin is not created until css is applied.

You could call applyCss after adding the TableView to a Scene and access the TableHeaderRow after this call.

Alternatively listen for changes in the Skin and execute that code after the skin has been set.

Furthermore I'd recommend using TableViewSkinBase.getTableHeaderRow to retrieve the header row instead of using lookup (you're using com.sun packages anyway).

tableView.skinProperty().addListener((a, b, newSkin) -> {
    TableHeaderRow headerRow = ((TableViewSkinBase) newSkin).getTableHeaderRow();
    ...
});
like image 100
fabian Avatar answered Nov 19 '22 14:11

fabian


lookup("TableHeaderRow"); works, but it needs called after the table is rendered or it will return null

like image 3
Zachary Avatar answered Nov 19 '22 13:11

Zachary