0

我有一个问题,如何将可扩展的 Threeview 插入 Accordion。

这是手风琴的代码:

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

1 回答 1

2

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

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

这会将节点 aNode 的每个角锚定到 AnchorPane 的角上。这是您在 AnchorPane 上选择“适合父级”选项时在 SceneBuilder 中获得的内容。

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

于 2013-04-28T17:42:22.123 回答