-1

我想用子节点创建 JavaFX。我设法创建了非常简单的树:

public class SQLBrowser extends Application {

    //////
    public List<ConnectionsListObj> connListObj = new ArrayList<>();

    public class ConnectionsListObj {

        private String connectionName;
        private String dbgwName;
        private String tableName;

        public ConnectionsListObj(String connectionName, String dbgwName, String tableName) {

            this.connectionName = connectionName;
            this.dbgwName = dbgwName;
            this.tableName = tableName;

        }

        public String getConnectionName() {
            return connectionName;
        }

        public void setConnectionName(String connectionName) {
            this.connectionName = connectionName;
        }

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

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Button Sample");
        stage.setWidth(300);
        stage.setHeight(190);
        VBox vbox = new VBox();
        vbox.setLayoutX(20);
        vbox.setLayoutY(20);

        ////////// Insert data

        connListObj.add(new ConnectionsListObj("Connection 1", "DBGW1", "Table 1"));
        connListObj.add(new ConnectionsListObj("Connection 1", "DBGW1", "Table 2"));
        connListObj.add(new ConnectionsListObj("Connection 1", "DBGW2", "Table 3"));
        connListObj.add(new ConnectionsListObj("Connection 1", "DBGW2", "Table 4"));

        //////////  Display data

        TreeItem<String> root = new TreeItem<>("Connection Name");
        root.setExpanded(true);

        for (ConnectionsListObj connection : connListObj) {
            // Add subnode DBGW name
            String DBName = connection.dbgwName;

            root.getChildren().addAll(new TreeItem<>(connection.dbgwName));

        }

        TreeView<String> treeView = new TreeView<>(root);

        /////////

        vbox.getChildren().add(treeView);
        vbox.setSpacing(10);
        ((Group) scene.getRoot()).getChildren().add(vbox);
        stage.setScene(scene);
        stage.show();
    }
} 

但我不知道如何在节点中创建子节点。我想与几个 DBGW 和每个 DBGW 建立一个连接,其中包含从 ArrayList 生成的表列表。

connListObj.add(new ConnectionsListObj("Connection 1", "DBGW1", "Table 1"));
connListObj.add(new ConnectionsListObj("Connection 1", "DBGW1", "Table 2"));
connListObj.add(new ConnectionsListObj("Connection 1", "DBGW2", "Table 3"));
connListObj.add(new ConnectionsListObj("Connection 1", "DBGW2", "Table 4"));

但是我如何创建一个循环来迭代到 ArrayList 并生成三个。

PS我以这种方式更新了代码:

TreeItem<String> root = new TreeItem<>("Connection Name");
        root.setExpanded(true);

        for (ConnectionsListObj connection : connListObj) {
            // Add subnode DBGW name
            String DBName = connection.dbgwName;

            TreeItem sb;

            root.getChildren().addAll(sb = new TreeItem<>(connection.dbgwName));

            //if (DBName.equals(oldDBName)) {

            sb.getChildren().add(new TreeItem<>(connection.tableName));

            //}

        }

        TreeView<String> treeView = new TreeView<>(root);

得到这个结果:

在此处输入图像描述

如何tables根据DBGW.

4

1 回答 1

1

有一个它的孩子的TreeItem列表。您必须向其中添加子节点:

parentNode.getChildren().add(yourNode);

有关完整示例,请参阅使用 JavaFX UI 控件 - 树视图

于 2013-04-29T10:15:44.553 回答