提问者:小点点

如何使用JavaFX将TreeView插入手风琴


我有一个问题如何插入3reeview这是可扩展到手风琴。

这是手风琴的代码:

public TitledPane createConnectionsTree(String title) {
    connectionsData = FXCollections.observableArrayList(connectionsList);
    ListView<ConnectionsObject> lv = new ListView<>(connectionsData);

    lv.setCellFactory(new Callback<ListView<ConnectionsObject>, ListCell<ConnectionsObject>>() {
        @Override
        public ListCell<ConnectionsObject> call(ListView<ConnectionsObject> p) {
            return new ConnectionsCellFactory();
        }
    });
    AnchorPane content = new AnchorPane();
    content.getChildren().add(lv);
    // add to TitelPane
    TitledPane pane = new TitledPane(title, content);
    return pane;
}

这是树形视图代码:

public void initTree() {
    rootNode.setExpanded(true);

    for (Employee employee : employees) {
        TreeItem<String> empLeaf = new TreeItem<>(employee.getName());
        boolean found = false;

        for (TreeItem<String> depNode : rootNode.getChildren()) {
            if (depNode.getValue().contentEquals(employee.getDepartment())) {
                depNode.getChildren().add(empLeaf);
                found = true;
                break;
            }
        }
        if (!found) {
            TreeItem depNode = new TreeItem(employee.getDepartment());
            rootNode.getChildren().add(depNode);
            depNode.getChildren().add(empLeaf);
        }
    }
    VBox box = new VBox();
    TreeView<String> treeView = new TreeView<>(rootNode);
    treeView.setShowRoot(true);
    treeView.setEditable(true);
    box.getChildren().add(treeView);
}

附言

我得到这样的结果:

我希望当我展开树视图时展开手风琴的滑块,而不是树视图的滑块。这是我测试的代码:

 public TitledPane createConnectionsList(String title) {

    rootNode.setExpanded(true);
    for (ThreeData conn : connectionsThree) {
        TreeItem<String> empLeaf = new TreeItem<>(conn.getName());
        boolean found = false;
        for (TreeItem<String> depNode : rootNode.getChildren()) {
            if (depNode.getValue().contentEquals(conn.getDepartment())) {
                depNode.getChildren().add(empLeaf);
                found = true;
                break;
            }
        }
        if (!found) {
            TreeItem depNode = new TreeItem(conn.getDepartment());
            rootNode.getChildren().add(depNode);
            depNode.getChildren().add(empLeaf);
        }
    }
    TreeView<String> treeView = new TreeView<>(rootNode);
    treeView.setShowRoot(true);
    treeView.setEditable(true);

    AnchorPane content = new AnchorPane();
    // Set aligment - fill the accordion with the three content
    AnchorPane.setLeftAnchor(treeView, 0d);
    AnchorPane.setRightAnchor(treeView, 0d);
    AnchorPane.setBottomAnchor(treeView, 0d);
    AnchorPane.setTopAnchor(treeView, 0d);

    content.getChildren().add(treeView);
    // Add to TitelPane
    TitledPane pane = new TitledPane(title, content);
    return pane;
}

共1个答案

匿名用户

当您使用FXML在SceneBuilder中设计GUI时,这个问题可能会更加明显。您需要使用锚定窗格来锚定节点,以便它们可以伸展。例如:

    AnchorPane.setLeftAnchor(aNode, 0d);
    AnchorPane.setRightAnchor(aNode, 0d);
    AnchorPane.setBottomAnchor(aNode, 0d);
    AnchorPane.setTopAnchor(aNode, 0d);

这将把节点阳极的每个角固定到锚定板的角上。这是在AnchorPane上选择“适合父级”选项时在SceneBuilder中得到的结果。

如果做不到这一点,只需使用 FXML,这将使获得您想要的 GUI 变得更加容易。