I'm currently trying to design a booking system. After selecting their preferred choices, the user will click "Confirm" and their data will be placed into a tableview. However, after I click back to return to homepage and returning back to the booking page, the data is all gone. Is there a way to preserve the data in the table after I leave the scene so that when I return back to the booking page, the data will still be
This is my Booking controller:
package application;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class CheckBookingController implements Initializable {
private Stage stage;
private Scene scene;
@FXML
private Button btnBack;
@FXML
private Button btnConfirm;
@FXML
private DatePicker btnDate;
@FXML
private Label dateStatus;
@FXML
private Label peopleStatus;
@FXML
private Label roomName;
@FXML
private TableView<Table> tableview;
@FXML
private ComboBox<String> timeBox;
@FXML
private ComboBox<String> peopleBox;
@FXML
private Label timeStatus;
@FXML
private TextField txtField;
@FXML
private TableColumn<Table, String> timeColumn;
@FXML
private TableColumn<Table, String> peopleColumn;
@FXML
private TableColumn<Table, String> roomColumn;
@FXML
private TableColumn<Table, String> dateColumn;
private String[] time = {"10-11","11-12","12-1"}; // values for combo box
private String[] people = {"1","2","3","4","5"}; //values for combo box
@FXML
public void onBackClicked(ActionEvent event) throws IOException { // back button to return home
Parent root = FXMLLoader.load(getClass().getResource("MainPage.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@FXML
void onBoxSelect(ActionEvent event) { //combo box with time
String theTime = timeBox.getValue();
timeStatus.setText(theTime);
}
@FXML
void onPeopleSelect(ActionEvent event) { //combo box with number of people
String thePeople = peopleBox.getValue();
peopleStatus.setText(thePeople);
}
@FXML
void onConfirmClicked(ActionEvent event) { //confirm button to store data into table
Table table = new Table (roomName.getText(), dateStatus.getText(), timeStatus.getText(), peopleStatus.getText()); //roomName is just a label to get text from
ObservableList<Table> tableAdd = tableview.getItems();
tableAdd.add(table);
tableview.setItems(tableAdd);
}
@FXML
void onDateClicked(ActionEvent event) { //date picker
LocalDate date = btnDate.getValue();
String formattedDate = date.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"));
dateStatus.setText(formattedDate);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
timeBox.getItems().addAll(time);
timeBox.setOnAction(this::onBoxSelect);
peopleBox.getItems().addAll(people);
peopleBox.setOnAction(this::onPeopleSelect);
timeColumn.setCellValueFactory(new PropertyValueFactory<Table, String>("time"));
dateColumn.setCellValueFactory(new PropertyValueFactory<Table, String>("date"));
roomColumn.setCellValueFactory(new PropertyValueFactory<Table, String>("room"));
peopleColumn.setCellValueFactory(new PropertyValueFactory<Table, String>("people"));
}
//after selecting data, the user clicks confirm and all the data from both combo box and date picker will be stored into the table.
}
This is my Table.java:
package application;
public class Table {
private String room;
private String date;
private String time;
private String people;
public Table(String room, String date, String time, String people){
this.room = room;
this.date = date;
this.time = time;
this.people = people;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPeople() {
return people;
}
public void setPeople(String people) {
this.people = people;
}
}