-1

TreeViewからJavaFXを構築する方法の解決策を探していArrayListます。このArrayList魔女には、接続名、データベースサーバー名、およびテーブルのリストが含まれています。

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

    }

ツリーを調べて、次のコードを使用してツリーを生成するある種のループが必要です。

TreeItem<String> treeItemConnections = new TreeItem<> ("Connections");

        TreeItem<String> nodeItemDBGW = new TreeItem<>("DBGW 1");

        treeItemConnections.getChildren().add(nodeItemDBGW);

            TreeItem<String> nodeItemTable = new TreeItem<>("Table 1");

            nodeItemDBGW.getChildren().add(nodeItemTable);

        TreeView<String> treeView = new TreeView<>(treeItemConnections);
        StackPane root = new StackPane();
        root.getChildren().add(treeView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

ArrayList問題は、 を調べて 3 つを構築するループをどのように作成できるかということです。また、ノードを選択すると、ノードのタイプを取得したいと思います。

4

1 に答える 1

1

ConnectionsListObj オブジェクトをツリーに配置しないのはなぜですか? TreeView は、各ツリーノードのテキストのオブジェクトで toString() を呼び出すと思うので、表示したい文字列を ConnectionsListObj.toString() から返すだけです。次に、呼び出して選択したアイテムmyTreeView.getSelectionModel().getSelectedItems()を取得すると、必要なすべてのデータを含む ConnectionsListObj のインスタンスが取得されます。

あなたのケースでは、Javaのループは次のようになります。

for(ConnectionsListObj connection : connListObj) {
    nodeItemDBGW.getChildren().add(connection);
}

また...

nodeItemDBGW.getChildren().addAll(connListObj);
于 2013-04-28T17:51:39.927 に答える