Пример кода обновления 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);
}
}
Person.java
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;
}
}
addcontroller.java
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();
}
}
rootcontroller.java
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);
}
}
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>
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.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>
Вы сохраняете новые данные в базе данных, но никогда не добавляете их в данные данных ObservableList, которые поддерживают TableView. – ItachiUchiha
Внутри 'update()' просто создайте новый экземпляр UserData с значениями текстового поля и добавьте его в 'data'. – ItachiUchiha
Проверьте [этот пример] (https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm#CJAFABCH). Ответ, который я добавил, ничего не делает. Если у вас возникли проблемы, создайте [Минимальный, полный и проверенный пример] (http://stackoverflow.com/help/mcve) и добавьте его в свой вопрос. – ItachiUchiha