如何创建JavaFX树子节点
我想创建带有子节点的JavaFX。我设法创建了非常简单的树:如何创建JavaFX树子节点,java,javafx-2,javafx,Java,Javafx 2,Javafx,我想创建带有子节点的JavaFX。我设法创建了非常简单的树: public class SQLBrowser extends Application { ////// public List<ConnectionsListObj> connListObj = new ArrayList<>(); public class ConnectionsListObj { private String connectionName;
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并生成三个
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);
TreeItem root=newtreeitem(“连接名”);
root.setExpanded(true);
for(ConnectionsListObj连接:connListObj){
//添加子节点DBGW名称
字符串DBName=connection.dbgwName;
TreeItem某人;
root.getChildren().addAll(sb=newtreeitem(connection.dbgwName));
//if(DBName.equals(oldDBName)){
sb.getChildren().add(新的TreeItem(connection.tableName));
//}
}
TreeView TreeView=新的TreeView(根);
得到以下结果:
如何根据
DBGW
对表进行排序树项
有其子项的列表。必须向其中添加子节点:
parentNode.getChildren().add(yourNode);
有关完整示例,请参阅。是,我成功创建了子节点。但现在的问题是如何正确排序?@PeterPenzov它们是按照您添加它们的顺序排序的。当我们在javafx2中添加/删除TreeItems(最好是在深度3之前)时,有没有办法保持TreeItems的排序?我可以在javafx8中看到一些SortedList,但遗憾的是,转移到jdk8对我来说是不可行的。当我做到这一点时,你需要一个dbgw1
项,然后table1
和table2
作为孩子,而不是两个dbgw1
。
parentNode.getChildren().add(yourNode);