0

我有这棵树,它会生成带有数据的小树。

public TitledPane createConnectionsList(String title) {

        TreeItem<String> rootNode = new TreeItem<>("Connection 1", null);

        rootNode.setExpanded(true);
        for (ConnectionData data : connectionDataList) {
            TreeItem<String> connLeaf = new TreeItem<>(data.getDBGWName());
            boolean found = false;
            for (TreeItem<String> depNode : rootNode.getChildren()) {
                if (depNode.getValue().contentEquals(data.getTableName())) {
                    depNode.getChildren().add(connLeaf);
                    found = true;
                    break;
                }
            }
            if (!found) {
                TreeItem<String> depNode = new TreeItem<>(data.getTableName(), null);
                rootNode.getChildren().add(depNode);
                depNode.getChildren().add(connLeaf);
            }
        }

        VBox box = new VBox();
        final Scene scene = new Scene(box, 700, 600);
        scene.setFill(Color.LIGHTGRAY);

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

public List<ConnectionData> connectionDataList;

    public static class ConnectionData {

        private String DBGWName;
        private String TableName;

        public ConnectionData(String DBGWName, String TableName) {

            this.DBGWName = DBGWName;
            this.TableName = TableName;

        }

        public String getDBGWName() {
            return DBGWName;
        }

        public void setDBGWName(String DBGWName) {
            this.DBGWName = DBGWName;
        }

        public String getTableName() {
            return TableName;
        }

        public void setTableName(String TableName) {
            this.TableName = TableName;
        }
    }

connectionDataList = Arrays.<ConnectionData>asList(
                new ConnectionData("Table 1", "DBGW1"),
                new ConnectionData("Table 2", "DBGW1"),
                new ConnectionData("Table 3", "DBGW1"),
                new ConnectionData("Table 4", "DBGW1"),
                new ConnectionData("Table 5", "DBGW1"),
                new ConnectionData("Table 6", "DBGW2"),
                new ConnectionData("Table 7", "DBGW2"),
                new ConnectionData("Table 8", "DBGW2"),
                new ConnectionData("Table 9", "DBGW2"),
                new ConnectionData("Table 10", "DBGW2"),
                new ConnectionData("Table 11", "DBGW2"),
                new ConnectionData("Table 12", "DBGW3"));

我想当我点击主节点Connection 1打开一个对话框。当我点击一个DBGW1子节点打开一个新的对话框窗口,当我点击一个子节点Table打开 3td 简单对话框时,我也想要。为了调用 Java 方法或刷新主阶段,我必须在哪里以及如何放置事件侦听器。

4

1 回答 1

0

让您了解情况: treeItem 是一个东西,它被添加到树结构中。稍后,会为每个 treeItem 创建一个 treeCell。从技术上讲,默认情况下,有一个默认的treeCellFactory,但您可以创建自己的工厂,这将为每个treeItem 创建一个TreeCell。

对于每个 treeCell,您可以手动添加一个侦听器,该侦听器将侦听交互事件,并对它们做出相应的反应。

但是,您的交互情况很简单:您可以在当前聚焦的节点或当前选定的节点上添加一个侦听器。(看来,重点节点是最好的选择)。

因此,使用其中一些方法:

    TreeView tv = new TreeView();
    tv.getFocusModel().focusedIndexProperty().addListener(...);
    tv.getFocusModel().focusedItemProperty().addListener(...);
    tv.getSelectionModel().selectedIndexProperty().addListener(...);
    tv.getSelectionModel().selectedItemProperty().addListener(...); 
于 2013-05-02T17:24:31.353 回答