2013-04-29 2 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(); 
    } 
} 

그러나 노드에 하위 노드를 만드는 방법을 알지 못한다. ArrayList에서 생성 된 테이블 목록을 사용하여 여러 DBGW 및 모든 DBGW와 하나의 연결을 만들고 싶습니다.

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. 내가 코드 이런 식으로 업데이트 :

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

DBGW에 따라 tables를 정렬하는 방법.

+0

이 권한을 얻으면 하나의'dbgw1' 항목과'dbgw1'이 아닌'table1'과'table2'을 자식으로 사용하십시오. – Kalaschni

+0

네, 맞습니다. –

답변

1

TreeItem에는 해당 자녀의 목록이 있습니다. 하위 노드를 추가해야합니다.

parentNode.getChildren().add(yourNode); 

전체 예제를 보려면 Using JavaFX UI Controls - Tree View을 참조하십시오.

+0

예 하위 노드를 생성 할 수있었습니다. 하지만 이제 문제는 내가 어떻게 제대로 정렬 할 수 있는가하는 것입니다. –

+0

@PeterPenzov 그들은 당신이 그들을 추가하는 순서대로 정렬됩니다. – Kai

+0

이 글을 쓰는 방법을 보여 주시겠습니까? –

관련 문제