How to get TableHeaderRow from TableView nowadays in JavaFX?

前端 未结 2 1544
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 19:03

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

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


        
相关标签:
2条回答
  • 2021-01-12 19:45

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

    0 讨论(0)
  • 2021-01-12 19:57

    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();
        ...
    });
    
    0 讨论(0)
提交回复
热议问题