2013-04-29 131 views
-1

我想創建具有子節點的JavaFX。我設法創建了非常簡單的樹:如何創建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並生成三個循環。

P.S.我更新的代碼是這樣的:

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

得到這樣的結果:

enter image description here

如何基於該DBGWtables排序。

+0

當我得到這個權利,你想要一個'dbgw1'項目,然後'table1'和'table2'作爲孩子,而不是兩個'dbgw1'。 – Kalaschni 2013-04-29 13:12:25

+0

是的,你是對的。 – 2013-04-29 13:45:50

回答

1

TreeItem有一個其子的列表。您必須添加子節點:

parentNode.getChildren().add(yourNode); 

有關完整示例,請參閱Using JavaFX UI Controls - Tree View

+0

是的,我設法創建了子節點。但現在的問題是我如何正確地對它們進行分類? – 2013-04-29 10:31:10

+0

@PeterPenzov他們按您添加它們的順序排序。 – Kai 2013-04-29 11:26:09

+0

你會告訴我如何寫這個? – 2013-04-29 11:49:17