gpt4 book ai didi

java - TreeItem 在 JavaFX 中被覆盖。怎么解决这个问题呢?

转载 作者:行者123 更新时间:2023-12-02 00:33:29 25 4
gpt4 key购买 nike

每次我添加新类时,Treeitem 都会被覆盖。怎么解决这个问题?

在添加新对象treeitem时应该动态增加尝试:

  1. 不使用列表添加
  2. 使用列表添加
  3. 尝试使用循环添加

这是一个示例代码,抱歉命名错误。提前致谢

--------标记问题地点

PanesClass.java

public class PanesClass extends Application {
ObservableList<Connections> cList = FXCollections.observableArrayList();

public static void main(String[] args) {
launch(args);
}

@SuppressWarnings("all")@Override
public void start(Stage primaryStage) throws Exception {
NewConnection newConnection = new NewConnection();
SplitPane root = new SplitPane();
AnchorPane first = new AnchorPane();
AnchorPane second = new AnchorPane();
TreeTableView activeConnections = new TreeTableView();
HBox buttonBox = new HBox();
BorderPane topBar = new BorderPane();
Button nConnection = new Button("+");
Button deleteConnection = new Button("X");
Button connect = new Button("Connect");

buttonBox.setSpacing(10);
buttonBox.getChildren().addAll(nConnection, deleteConnection, connect);
topBar.setTop(buttonBox);

TreeTableColumn<String, Connections > cNameColoumn = new TreeTableColumn<>("Name");

cNameColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cname"));

TreeTableColumn<String, Connections> cStatusColoumn = new TreeTableColumn<>("Status");
cStatusColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cstatus"));

activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
activeConnections.setLayoutX(20);
activeConnections.setLayoutY(40);
activeConnections.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

first.getChildren().addAll(topBar, activeConnections);
root.getItems().addAll(first, second);

Scene sc = new Scene(root, 600, 480);

primaryStage.setScene(sc);
primaryStage.show();

nConnection.setOnAction(new EventHandler<ActionEvent>() {@Override
public void handle(ActionEvent event) {
newConnection.getConnection(activeConnections);
}
});
}
}

NewConnection.java

public class NewConnection {
Connections connection = null;
ObservableList<Connections> cList = FXCollections.observableArrayList();
PanesClass panesClass = new PanesClass();
TreeItem cItem = null;
TreeItem nItem = null;

public void getConnection(TreeTableView<Connections> activeConnections) {
Stage secondaryStage = new Stage();
VBox root = new VBox();
GridPane cDetails = new GridPane();
HBox actionButtons = new HBox();
Button connect = new Button("Connect");
Button save = new Button("Save");
Button cancel = new Button("Cancel");

actionButtons.getChildren().addAll(connect, save, cancel);
actionButtons.setSpacing(10);

Label name = new Label("Username : ");

cDetails.add(name, 0, 0);

TextField uName = new TextField();

cDetails.setHgrow(uName, Priority.ALWAYS);
cDetails.add(uName, 1, 0);

Label password = new Label("Password : ");

cDetails.add(password, 0, 1);

TextField pwd = new TextField();

cDetails.add(pwd, 1, 1);

Label urllink = new Label("URL : ");

cDetails.add(urllink, 0, 2);

TextField url = new TextField();

cDetails.add(url, 1, 2);
cDetails.setVgap(10);
cDetails.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 1;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: black;");
root.getChildren().addAll(cDetails, actionButtons);

Scene sc = new Scene(root, 500, 200);

secondaryStage.setScene(sc);
secondaryStage.initModality(Modality.APPLICATION_MODAL);
secondaryStage.show();

save.setOnAction(new EventHandler<ActionEvent>() {
//*-----------------------------------------------------------------------*
@Override
public void handle(ActionEvent event) {
cItem = getitem(cItem);
activeConnections.setRoot(cItem);
activeConnections.setShowRoot(false);
secondaryStage.close();
}

private TreeItem getitem(TreeItem cItem) {
cList.add(new Connections(uName.getText()));
System.out.println(cList);

for (Connections temp: cList) {
System.out.println(temp);
nItem = new TreeItem<Connections>(temp);
System.out.println(nItem);
cItem.getChildren().add(nItem);
}
return cItem;
}
});
System.out.println(cList);
}
}

连接.java

public class Connections {
private String cname = null;
private String cstatus = null;
private String cpwd = null;
private String curl = null;

public Connections() {

}

public Connections(String cname, String cpwd, String curl) {
super();
this.cname = cname;
this.cpwd = cpwd;
this.curl = curl;
}

public Connections(String cname, String cstatus) {
super();
this.cname = cname;
this.cstatus = cstatus;
}

public String getCpwd() {
return cpwd;
}

public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}

public String getCurl() {
return curl;
}

public void setCurl(String curl) {
this.curl = curl;
}

public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCstatus() {
return cstatus;
}
public void setCstatus(String cstatus) {
this.cstatus = cstatus;
}

@Override
public String toString() {
return "Connections [cname=" + cname + ", cstatus=" + cstatus + ", cpwd=" + cpwd + ", curl=" + curl + "]";
}
}

最佳答案

您不是在填充树,而是在创建新项目而不将它们添加到树中。首先,您需要创建一个根:

// Instead of this line
// TreeItem nItem = null;
TreeItem rootItem = new TreeItem();

然后:

activeConnections.setRoot(rootItem);
save.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// clear old connections
rootItem.getChildren().clear();

// Add new connection
cList.add(new Connections(uName.getText(), pwd.getText(), url.getText()));

// create new items and add them to rootItem
for (Connections temp : cList) {
rootItem.getChildren().add(new TreeItem<Connections>(temp));
}

secondaryStage.close();
event.consume();
}
});

注意:如果您没有其他原因保留cList,您可以将其删除并直接添加新项目(无需每次都清除并重新生成项目) ):

save.setOnAction(event -> {
Connections newConnection = new Connections(uName.getText(), pwd.getText(), url.getText());
rootItem.getChildren().add(new TreeItem<>(newConnection));

secondaryStage.close();
event.consume();
});

关于java - TreeItem 在 JavaFX 中被覆盖。怎么解决这个问题呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60634609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com