如何使用JavaFX将TreeView插入到accordion中

时间:2013-04-26 11:19:16

标签: java javafx-2 javafx

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

这是手风琴的代码:

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

P.S

我得到了这个结果:

enter image description here

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

 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 个答案:

答案 0 :(得分:2)

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

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

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

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