How to get TableHeaderRow from TableView nowadays in JavaFX?

霸气de小男生 提交于 2019-12-30 09:29:27

问题


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?


回答1:


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();
    ...
});



回答2:


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



来源:https://stackoverflow.com/questions/38718926/how-to-get-tableheaderrow-from-tableview-nowadays-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!