我制作了一个高度为 40 px 的自定义树单元。
这里的问题披露三角形没有垂直居中对齐。
这里是树单元的代码:
public static class TestObjectCell extends AnchorPane implements ISMPBVisualComponentWithData<TestObject>{
public Label label;
public TestObjectCell(){
label=new Label("label");
AnchorPane.setTopAnchor(label, 10.0);
this.getChildren().setAll(label);
this.setMinHeight(40);
this.setPrefHeight(40);
this.setMaxHeight(40);
}
@Override
public void setComponentData(TestObject object) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
正如您在顶部对齐的图片显示三角形中看到的那样,如何对齐单元格的垂直中心?
最佳答案
我也遇到了同样的问题。我使用单元工厂来翻译 TreeCell 的公开节点:
myTree.setCellFactory(treeView -> {
final TreeCell<Label> cell = new TreeCell<Label>() {
@Override public void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
final Node graphic = (getTreeItem() == null) ? null : getTreeItem().getGraphic();
setGraphic((graphic != null) ? graphic : (item != null) ? item.getGraphic() : null);
}
setText((empty || item == null) ? null : item.getText());
}
};
cell.disclosureNodeProperty().addListener((obs, oldNode, newNode) -> {
final StackPane pane = (StackPane) newNode;
newNode.translateYProperty().bind(cell.heightProperty().multiply(0.5).subtract(pane.heightProperty()));
});
return cell;
});
当然必须有更好的方法来做到这一点,但至少这对我有用。
关于treeview - JDK 8 TreeView 披露三角形垂直对齐不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369438/