单击树节点时如何刷新阶段

时间:2013-04-30 08:12:10

标签: java javafx-2 javafx

我有这棵树,用数据生成小树。

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方法或刷新主要阶段。

1 个答案:

答案 0 :(得分: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(...);