2013-04-26 68 views
-1

我正在尋找解決方案,如何從ArrayList構建JavaFX TreeView。我有這個ArrayList巫包含連接名稱,數據庫服務器名稱和目錄表:如何從Arraylist構建TreeView

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並構建三個?而且當我選擇一個節點時,我想獲得節點的類型。

+0

如果數據庫DB1有兩個表的數組列表,將有2項是什麼呢? (具有相同的數據庫詳細信息但表名不同?) – 2013-04-26 15:21:13

+0

一個連接,一個數據庫與一個表? – tarrsalah 2013-04-26 15:58:46

+0

這只是一個例子。 – 2013-04-26 17:12:26

回答

1

爲什麼不直接將ConnectionsListObj對象放在樹中?我認爲TreeView在每個樹節點中的文本的對象上調用toString(),所以只需返回要從ConnectionsListObj.toString()中顯示的字符串。然後當你通過調用myTreeView.getSelectionModel().getSelectedItems()得到選定的項目時,你會得到ConnectionsListObj的一個實例,它應該有你需要的所有數據。

循環在Java中看起來像你的情況如下:

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

或...

nodeItemDBGW.getChildren().addAll(connListObj);