问题
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