如何在 treetableview 中设置第一级 child 的样式

How to set style for first level child in treetableview

我想为所有第一个 child 设置上边框,这样 child 中的一个数据就会排成一行。 但我无法为这些行设置 id。我尝试了以下代码,但没有帮助。

final PseudoClass firstRowClass = PseudoClass.getPseudoClass("first-child");

devWorkTabBenchtable.setRowFactory(new Callback<TreeTableView<DevWorkTabBench>, TreeTableRow<DevWorkTabBench>>() {

    @Override
    public TreeTableRow<DevWorkTabBench> call(TreeTableView<DevWorkTabBench> param) {
        TreeTableRow<DevWorkTabBench> row = new TreeTableRow<>();
        row.treeItemProperty().addListener(new ChangeListener<TreeItem<DevWorkTabBench>>() {
            int i = 0;

            @Override
            public void changed(ObservableValue<? extends TreeItem<DevWorkTabBench>> observable, TreeItem<DevWorkTabBench> oldValue, TreeItem<DevWorkTabBench> newValue) {
                if (i < param.getRoot().getChildren().size()) {
                    row.pseudoClassStateChanged(firstRowClass, newValue == param.getRoot().getChildren().get(i));
                }
                i++;
            }
        });
        return row;
    }
});

我应该使用什么?请指导我。

试试这个

treeTableView.setRowFactory( new Callback<TreeTableView<DevWorkTabBench>, TreeTableRow<DevWorkTabBench>>()
{
    @Override
    public TreeTableRow<DevWorkTabBench> call( TreeTableView<DevWorkTabBench> param )
    {
        TreeTableRow<DevWorkTabBench> row = new TreeTableRow<>();
        row.treeItemProperty().addListener( new ChangeListener<TreeItem<DevWorkTabBench>>()
        {
            @Override
            public void changed( ObservableValue<? extends TreeItem<DevWorkTabBench>> observable, TreeItem<DevWorkTabBench> oldValue, TreeItem<DevWorkTabBench> newValue )
            {
                row.pseudoClassStateChanged( firstRowClass, param.getRoot().getChildren().contains( newValue ) );
            }
        } );
        return row;
    }
} );

How can I apply multiple pseudo classes for different levels?

您可以获得 TreeItemvalueProperty 并决定应用哪个 css class:

DevWorkTabBench bench = newValue.getValue();

row.pseudoClassStateChanged( thirdLevelClass, bench.getLevel() == 3 );
// or
row.pseudoClassStateChanged( MyCSSUtils.getClassByLevel(bench.getLevel()), true );