Reputation: 79
I am working on a JAVAFX
application. In my application, after clicking a button, it opens one window which has a TableView
in it with one Apply
and Save
button. When clicking on the Apply
button, it will preserve the current state of the TableView
(in case we add/remove the table rows and click apply and reopen the table the previously updated TableView
should be shown). The Save
button is to save the table records to a database. Suppose there are two rows (coming from the database) in the table and if I add a 3rd row and click Apply
my TableView
window will be closed. And If I reopen the table the third row is not present.
How do I preserve that previously added third row, without inserting it into the database?
Upvotes: 4
Views: 2133
Reputation: 21799
You could try to store the items of the TableView
(or optionally just the list of the added items of the TableView
) as a member in the class that opens the window.
I have created an example:
The application TableViewSample
can be used to open a second window. This application stores an instance of TablePopUp
which class can show a second modal Stage
, while maintaining a "buffer" - a list of Person
(the data model what is displayed on the TableView
) object, that were added to the TableView
and were "Applied" but not stored in the database yet.
public class TableViewSample extends Application {
// Stores the state of the TableView and opens the second window
TablePopUp popUp = new TablePopUp();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new BorderPane());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);
BorderPane root = (BorderPane) scene.getRoot();
Button button = new Button("Open window");
button.setOnAction((e) -> popUp.showTable());
root.setCenter(button);
stage.setScene(scene);
stage.show();
}
class TablePopUp {
// Stores the Person object which were added and applied but not stored
// in DB
ObservableList<Person> bufferAdd = FXCollections.observableArrayList();
// Simulate the items coming from the DV
private ObservableList<Person> dataFromDB = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]"));
void showTable() {
// Temporary buffer for the added Persion objects
ObservableList<Person> tempBuffer = FXCollections.observableArrayList();
// Temporary buffer to store persons to be deleted on apply
ObservableList<Person> bufferRemoveFromBuffer = FXCollections.observableArrayList();
// Data what the TableView displays
ObservableList<Person> tableData = FXCollections.observableArrayList();
// Stores the person objects that will be removed from the DB if Save is pressed
ObservableList<Person> bufferRemoveFromDB = FXCollections.observableArrayList();
// The Table displays elements from the DB + the applied buffer
tableData.addAll(dataFromDB);
tableData.addAll(bufferAdd);
// Create the table
TableView<Person> table = new TableView<Person>();
table.setItems(tableData);
Scene scene = new Scene(new BorderPane());
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setPromptText("Last Name");
TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setPromptText("Email");
// Button to add a new Person
Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Person newPerson = new Person(addFirstName.getText(), addLastName.getText(), addEmail.getText());
// Add a new element to the temporary buffer and add it to
// the table data also
tempBuffer.add(newPerson);
tableData.add(newPerson);
addFirstName.clear();
addLastName.clear();
addEmail.clear();
}
});
// Button to remove a Person
Button removeButton = new Button("Remove");
removeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Person selectedItem = table.getSelectionModel().getSelectedItem();
if(selectedItem != null) {
// Remove the item from the list of the displayed persons
tableData.remove(selectedItem);
// Check the buffers: if one of the buffer contains the selected item, remove it from the buffer
if(tempBuffer.contains(selectedItem))
tempBuffer.remove(selectedItem);
else if(bufferAdd.contains(selectedItem))
bufferRemoveFromBuffer.add(selectedItem);
else {
// The item is not in the buffers -> remove the item from the DB
bufferRemoveFromDB.add(selectedItem);
}
}
}
});
HBox hb = new HBox();
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, removeButton);
hb.setSpacing(3);
VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
BorderPane root = (BorderPane) scene.getRoot();
root.setCenter(vbox);
Stage stage = new Stage();
HBox applySave = new HBox();
// On Save:
// Remove all elements from the buffer that were selected to be deleted
// Remove all elements from the BD that were selected to be deleted
// Add all the elements from the persistent buffer to the DB
// Add all the elements from the temporary buffer to the DB
// Clear both buffers
Button saveButton = new Button("Save to DB");
saveButton.setOnAction((e) -> {
bufferAdd.removeAll(bufferRemoveFromBuffer);
dataFromDB.removeAll(bufferRemoveFromDB);
dataFromDB.addAll(bufferAdd);
dataFromDB.addAll(tempBuffer);
bufferAdd.clear();
stage.close();
});
// On Apply:
// Add elements from the temporary buffer to the persistent buffer
// Remove elements from the buffer
Button applyButton = new Button("Apply");
applyButton.setOnAction((e) -> {
bufferAdd.addAll(tempBuffer);
bufferAdd.removeAll(bufferRemoveFromBuffer);
stage.close();
});
applySave.getChildren().addAll(saveButton, applyButton);
root.setBottom(applySave);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.show();
}
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
}
The class TablePopUp
has actually two buffers: a temporary buffer, which is used to stored the added the elements, and a persistent one which is kept between the different window openings. If the "Apply" button is pressed, the temporary buffer is stored in the persistent one. If the "Save" button is pressed, both buffers are stored in the DB then they gets cleared.
In the example the removing is also buffered. On remove it finds out, that the Person
object that is selected to be removed coming from the database or not. If it coming from the database, it placed to a buffer and it only gets removed from the database, if the save button is pressed. The same workflow is valid for the persons that are added but not placed into the database yet: on remove they only get removed, if the apply button is pressed.
Upvotes: 2