我想創建具有子節點的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);
得到這樣的結果:
如何基於該DBGW
的tables
排序。
當我得到這個權利,你想要一個'dbgw1'項目,然後'table1'和'table2'作爲孩子,而不是兩個'dbgw1'。 – Kalaschni 2013-04-29 13:12:25
是的,你是對的。 – 2013-04-29 13:45:50