하위 노드로 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);
이 결과를 얻을 :
가DBGW
에 따라
tables
를 정렬하는 방법.
이 권한을 얻으면 하나의'dbgw1' 항목과'dbgw1'이 아닌'table1'과'table2'을 자식으로 사용하십시오. – Kalaschni
네, 맞습니다. –