Javafx 为什么我的TableView没有刷新或自动更新?
我尝试了很多次,但是当单击“添加”按钮时,我在文本字段中输入的值没有填充到tableview中。请给我一个解决方案,以便在每次单击“添加”按钮时将值填充到tableview中Javafx 为什么我的TableView没有刷新或自动更新?,javafx,javafx-2,javafx-8,auto-update,Javafx,Javafx 2,Javafx 8,Auto Update,我尝试了很多次,但是当单击“添加”按钮时,我在文本字段中输入的值没有填充到tableview中。请给我一个解决方案,以便在每次单击“添加”按钮时将值填充到tableview中 public class Refreshtable extends Application { @FXML private TextField fname; private final TableView<Person> table = new 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);
}
}
}
公共类刷新表扩展应用程序{
@FXML
私有文本字段fname;
private final TableView table=new TableView();
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”);
最终HBox hb=新HBox();
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primarystage){
场景=新场景(新组());
//this.primaryStage.setTitle(“表视图示例”);
初生阶段:坐骨宽度(450);
初生阶段,坐骨高度(550);
最终标签=新标签(“地址簿”);
label.setFont(新字体(“Arial”,20));
table.setEditable(true);
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
新财产价值工厂(“名字”);
表2.设置项目(数据);
table.getColumns().addAll(firstNameCol);
最终按钮addButton=新按钮(“添加”);
addButton.setOnAction((ActionEvent e)->{
系统输出打印项次(“u输入”);
试一试{
FXMLLoader=新的FXMLLoader();
setLocation(Refreshtable.class.getResource(“newaddframe.fxml”);
AnchorPane AnchorPane=(AnchorPane)loader.load();
Stage dialogStage=新阶段();
dialogStage.setTitle(“主”);
dialogStage.InitModal(MODAL.WINDOW_MODAL);
场景场景=新场景(锚烷);
dialogStage.setScene(场景);
dialogStage.showAndWait();
}捕获(IOES异常){
es.printStackTrace();}
});
hb.getChildren().addAll(addButton);
最终VBox VBox=新的VBox();
vbox.setspace(5);
设置填充(新的插入(10,0,0,10));
vbox.getChildren().addAll(表,hb);
((组)scene.getRoot()).getChildren().addAll(vbox);
初级阶段。场景(场景);
primarystage.show();
}
@FXML
私有void addd(){
数据。添加(新人员)(
fname.getText());
fname.clear();
}
公共静态类人员{
私有最终SimpleStringProperty名字;
私人(字符串fName){
this.firstName=新的SimpleStringProperty(fName);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串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);
}
刷新tableview代码示例: Cloud.java
public class Cloud extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
/**
* The data as an observable list of Persons.
*/
private ObservableList<Person> personData = FXCollections.observableArrayList();
/**
* Constructor
*/
public Cloud() {
}
/**
* Returns the data as an observable list of Persons.
* @return
*/
public ObservableList<Person> getPersonData() {
return personData;
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
// Set the application icon.
this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));
showPersonOverview();
}
/**
* Initializes the root layout and tries to load the last opened
* person file.
*/
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("root.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
Stage dialogStagee = new Stage();
dialogStagee.setTitle("Edit Person");
dialogStagee.initModality(Modality.WINDOW_MODAL);
dialogStagee.initOwner(primaryStage);
Scene scene = new Scene(personOverview);
dialogStagee.setScene(scene);
// Give the controller access to the main app.
rootcontroller controller = loader.getController();
controller.setMainApp(this);
dialogStagee.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Opens a dialog to edit details for the specified person. If the user
* clicks OK, the changes are saved into the provided person object and true
* is returned.
*
* @param person the person object to be edited
* @return true if the user clicked OK, false otherwise.
*/
public boolean showPersonEditDialog(Person person) {
try {
// Load the fxml file and create a new stage for the popup dialog.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("add.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Person");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the person into the controller.
addcontroller controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setPerson(person);
// Set the dialog icon.
dialogStage.getIcons().add(new Image("file:resources/images/edit.png"));
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
public class addcontroller {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
private Stage dialogStage;
private Person person;
// private boolean okClicked = false;
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the person to be edited in the dialog.
*
* @param person
*/
public void setPerson(Person person) {
this.person = person;}
@FXML
private void handleOk() {
person.setFirstName(firstNameField.getText());
person.setLastName(lastNameField.getText());
dialogStage.close();
}
}
public class rootcontroller {
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
// Reference to the main application.
private Cloud mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public rootcontroller() {
}
@FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
}
public void setMainApp(Cloud mainApp) {
this.mainApp = mainApp;
personTable.setItems(mainApp.getPersonData());
}
@FXML
private void handleNewPerson() {
Person tempPerson = new Person();
System.out.println("1");
mainApp.showPersonEditDialog(tempPerson);
mainApp.getPersonData().add(tempPerson);
}
}
addcontroller.java
public class Cloud extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
/**
* The data as an observable list of Persons.
*/
private ObservableList<Person> personData = FXCollections.observableArrayList();
/**
* Constructor
*/
public Cloud() {
}
/**
* Returns the data as an observable list of Persons.
* @return
*/
public ObservableList<Person> getPersonData() {
return personData;
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
// Set the application icon.
this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));
showPersonOverview();
}
/**
* Initializes the root layout and tries to load the last opened
* person file.
*/
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("root.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
Stage dialogStagee = new Stage();
dialogStagee.setTitle("Edit Person");
dialogStagee.initModality(Modality.WINDOW_MODAL);
dialogStagee.initOwner(primaryStage);
Scene scene = new Scene(personOverview);
dialogStagee.setScene(scene);
// Give the controller access to the main app.
rootcontroller controller = loader.getController();
controller.setMainApp(this);
dialogStagee.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Opens a dialog to edit details for the specified person. If the user
* clicks OK, the changes are saved into the provided person object and true
* is returned.
*
* @param person the person object to be edited
* @return true if the user clicked OK, false otherwise.
*/
public boolean showPersonEditDialog(Person person) {
try {
// Load the fxml file and create a new stage for the popup dialog.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("add.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Person");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the person into the controller.
addcontroller controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setPerson(person);
// Set the dialog icon.
dialogStage.getIcons().add(new Image("file:resources/images/edit.png"));
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
public class addcontroller {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
private Stage dialogStage;
private Person person;
// private boolean okClicked = false;
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the person to be edited in the dialog.
*
* @param person
*/
public void setPerson(Person person) {
this.person = person;}
@FXML
private void handleOk() {
person.setFirstName(firstNameField.getText());
person.setLastName(lastNameField.getText());
dialogStage.close();
}
}
public class rootcontroller {
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
// Reference to the main application.
private Cloud mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public rootcontroller() {
}
@FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
}
public void setMainApp(Cloud mainApp) {
this.mainApp = mainApp;
personTable.setItems(mainApp.getPersonData());
}
@FXML
private void handleNewPerson() {
Person tempPerson = new Person();
System.out.println("1");
mainApp.showPersonEditDialog(tempPerson);
mainApp.getPersonData().add(tempPerson);
}
}
rootcontroller.java
public class Cloud extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
/**
* The data as an observable list of Persons.
*/
private ObservableList<Person> personData = FXCollections.observableArrayList();
/**
* Constructor
*/
public Cloud() {
}
/**
* Returns the data as an observable list of Persons.
* @return
*/
public ObservableList<Person> getPersonData() {
return personData;
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
// Set the application icon.
this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));
showPersonOverview();
}
/**
* Initializes the root layout and tries to load the last opened
* person file.
*/
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("root.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
Stage dialogStagee = new Stage();
dialogStagee.setTitle("Edit Person");
dialogStagee.initModality(Modality.WINDOW_MODAL);
dialogStagee.initOwner(primaryStage);
Scene scene = new Scene(personOverview);
dialogStagee.setScene(scene);
// Give the controller access to the main app.
rootcontroller controller = loader.getController();
controller.setMainApp(this);
dialogStagee.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Opens a dialog to edit details for the specified person. If the user
* clicks OK, the changes are saved into the provided person object and true
* is returned.
*
* @param person the person object to be edited
* @return true if the user clicked OK, false otherwise.
*/
public boolean showPersonEditDialog(Person person) {
try {
// Load the fxml file and create a new stage for the popup dialog.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Cloud.class.getResource("add.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Person");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the person into the controller.
addcontroller controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setPerson(person);
// Set the dialog icon.
dialogStage.getIcons().add(new Image("file:resources/images/edit.png"));
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
public class addcontroller {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
private Stage dialogStage;
private Person person;
// private boolean okClicked = false;
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the person to be edited in the dialog.
*
* @param person
*/
public void setPerson(Person person) {
this.person = person;}
@FXML
private void handleOk() {
person.setFirstName(firstNameField.getText());
person.setLastName(lastNameField.getText());
dialogStage.close();
}
}
public class rootcontroller {
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
// Reference to the main application.
private Cloud mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public rootcontroller() {
}
@FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
}
public void setMainApp(Cloud mainApp) {
this.mainApp = mainApp;
personTable.setItems(mainApp.getPersonData());
}
@FXML
private void handleNewPerson() {
Person tempPerson = new Person();
System.out.println("1");
mainApp.showPersonEditDialog(tempPerson);
mainApp.getPersonData().add(tempPerson);
}
}
公共类根控制器{
@FXML
个人桌面视图;
@FXML
private TableColumn firstNameColumn;
@FXML
私有TableColumn lastNameColumn;
//对主应用程序的引用。
私有云mainApp;
/**
*构造器。
*在initialize()方法之前调用构造函数。
*/
公共根控制器(){
}
@FXML
私有void初始化(){
//使用这两列初始化person表。
firstNameColumn.setCellValueFactory(cellData->cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData->cellData.getValue().lastNameProperty());
}
公共void setMainApp(云mainApp){
this.mainApp=mainApp;
setItems(mainApp.getPersonData());
}
@FXML
私有无效handleNewPerson(){
Person tempPerson=新的Person();
系统输出打印项次(“1”);
mainApp.showPersonEditDialog(tempPerson);
mainApp.getPersonData().add(tempPerson);
}
}
root.fxml
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.rootcontroller">
<children>
<TableView fx:id="personTable" layoutX="215.0" layoutY="106.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="name" />
<TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="country" />
</columns>
</TableView>
<Button layoutX="415.0" layoutY="334.0" mnemonicParsing="false" onAction="#handleNewPerson" text="add" />
</children>
</AnchorPane>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.addcontroller">
<children>
<TextField fx:id="firstNameField" layoutX="97.0" layoutY="113.0" />
<TextField fx:id="lastNameField" layoutX="259.0" layoutY="113.0" />
<Button layoutX="451.0" layoutY="113.0" mnemonicParsing="false" onAction="#handleOk" text="save" />
</children>
</AnchorPane>
add.fxml
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.rootcontroller">
<children>
<TableView fx:id="personTable" layoutX="215.0" layoutY="106.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="name" />
<TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="country" />
</columns>
</TableView>
<Button layoutX="415.0" layoutY="334.0" mnemonicParsing="false" onAction="#handleNewPerson" text="add" />
</children>
</AnchorPane>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cloud.addcontroller">
<children>
<TextField fx:id="firstNameField" layoutX="97.0" layoutY="113.0" />
<TextField fx:id="lastNameField" layoutX="259.0" layoutY="113.0" />
<Button layoutX="451.0" layoutY="113.0" mnemonicParsing="false" onAction="#handleOk" text="save" />
</children>
</AnchorPane>
您正在将新数据保存到数据库中,但从未将其添加到支持TableView的ObservableListdata
中。在更新()中
只需使用文本字段值创建一个新的UserData实例,并将其添加到数据中即可。签出。我添加的答案没有任何额外的作用。如果您在运行时遇到问题,请创建一个并将其添加到您的问题中。@ItachiUchiha使用上述示例可以在该框架内将项目添加到tableview中。我的要求是从上图所示的新框架中添加项目。plz帮助我将dialogStage
的实例传递到控制器中不是一个很好的解决方案。更好的解决方案是在控制器内定义布尔属性
,并将dialogStage close属性绑定到该属性。