我想用子节点创建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);
获得此结果:
如何根据tables
对DBGW
进行排序。
答案 0 :(得分:1)
TreeItem
有一个孩子的列表。您必须将子节点添加到它:
parentNode.getChildren().add(yourNode);
有关完整示例,请参阅Using JavaFX UI Controls - Tree View。