gpt4 book ai didi

javafx - 为什么我的 TableView 没有刷新或自动更新?

转载 作者:行者123 更新时间:2023-12-01 11:29:48 25 4
gpt4 key购买 nike

Simply when i add a textfield and clicked on add button my value is not getting inserted to tableview

我尝试了很多,但是当单击添加按钮时,我在文本字段中输入的值没有填充到 TableView 中。请给我一个解决方案,以便在我单击添加按钮时将值填充到 TableView 中!

 public class Refreshtable  extends Application {
@FXML
private TextField fname;
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob"));
final HBox hb = new HBox();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primarystage) {
Scene scene = new Scene(new Group());
// this.primaryStage.setTitle("Table View Sample");
primarystage.setWidth(450);
primarystage.setHeight(550);

final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));

table.setEditable(true);

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory("firstName"));
table.setItems(data);
table.getColumns().addAll(firstNameCol);
final Button addButton = new Button("Add");
addButton.setOnAction((ActionEvent e) -> {
System.out.println("u entered");
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Refreshtable.class.getResource("newaddframe.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Main");
dialogStage.initModality(Modality.WINDOW_MODAL);
Scene scenee = new Scene(anchorpane);
dialogStage.setScene(scenee);
dialogStage.showAndWait();
} catch (IOException es) {
es.printStackTrace();}
});
hb.getChildren().addAll(addButton);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll( table, hb);

((Group) scene.getRoot()).getChildren().addAll(vbox);

primarystage.setScene(scene);
primarystage.show();
}
@FXML
private void addd(){
data.add(new Person(
fname.getText()));
fname.clear();

}
public static class Person {
private final SimpleStringProperty firstName;
private Person(String fName) {
this.firstName = new SimpleStringProperty(fName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}


}
}

最佳答案

我不会谈论您的代码的(许多)问题,并保持简短的回答,解决您的原始问题。

您需要将用户输入的数据添加到支持 TableView 的 ObservableList。

@FXML
private void update()
{
...
// Create new instance of UserData
UserData userData = new UserData(name.getText(), country.getText());
// Add it to the backing list
data.add(userData);
}

关于javafx - 为什么我的 TableView 没有刷新或自动更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33432239/

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