简体   繁体   English

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

[英]How to insert TreeView into accordion using JavaFX

I have a problem how to insert Threeview which is expandable into Accordion. 我有一个问题如何插入可以扩展到Accordion的Threeview。

This is the code of the 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;
}

This is the treeview code: 这是树视图代码:

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

PS PS

I get this result: 我得到这个结果:

在此输入图像描述

I want when I expand the treeview to expand the slider of the accordion not the slider of the treeview. 我想扩展树视图以扩展手风琴的滑块而不是树视图的滑块。 This is the code that I tested: 这是我测试的代码:

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

The problem is probably more obvious when you're designing your GUI in SceneBuilder with FXML. 当您使用FXML在SceneBuilder中设计GUI时,问题可能更明显。 You need to anchor nodes with an AnchorPane so that they stretch out. 您需要使用AnchorPane锚定节点,以便它们伸展出来。 For example: 例如:

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

This will anchor each corner of node aNode to the corners of the AnchorPane. 这会将节点aNode的每个角锚定到AnchorPane的角落。 This is what you get in SceneBuilder when selecting the "Fit to Parent" option on an AnchorPane. 这是在AnchorPane上选择“适合父级”选项时在SceneBuilder中获得的。

Failing that, just use FXML which will make it much easier to get the GUI you want. 如果做不到这一点,只需使用FXML,这将使您更容易获得所需的GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM