How to set style for first level child in treetableview

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:37:18

问题


I want to set the top border just for all the first child so that for one child the data looks in one row. but I'm unable to set the id for those rows. I tried the following code but it didn't help.

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

What should I use? please guide me.


回答1:


Try this

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?

You can get the valueProperty of the TreeItem and decide which css class to apply:

DevWorkTabBench bench = newValue.getValue();

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


来源:https://stackoverflow.com/questions/30372170/how-to-set-style-for-first-level-child-in-treetableview

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