【问题标题】:JDK 8 TreeView disclosure triangle vertical aligned not correctlyJDK 8 TreeView 披露三角形垂直对齐不正确
【发布时间】:2013-12-04 07:23:57
【问题描述】:

我制作了一个高度为 40 像素的自定义树单元格。

这里的问题披露三角形没有垂直对齐中心。

这里的树细胞代码:

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.
        }



    }

正如您在图片披露三角形顶部看到的那样,如何对齐单元格的垂直中心?

【问题讨论】:

    标签: treeview javafx-2 javafx java-8


    【解决方案1】:

    我遇到了同样的问题。我用细胞工厂翻译了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;
    });
    

    当然必须有更好的方法来做到这一点,但至少这对我有用。

    【讨论】:

    • 对我来说这些参数更理想:newNode.translateYProperty().bind(cell.heightProperty().multiply(0.5).subtract(pane.heightProperty().divide(1.25)));
    【解决方案2】:

    你试过设置alignment property inside TreeCell吗?

    【讨论】:

      【解决方案3】:

      我发现更改显示节点对齐方式的唯一方法是使用 -fx-padding 样式手动覆盖垂直填充:

      .tree-disclosure-node {
         -fx-padding: 0.85em 0.229em 0.333333em 0.229em;
      }
      

      这将允许您根据需要使箭头居中,但如果行高发生变化,您将需要修改此值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-30
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        相关资源
        最近更新 更多