I have implemented what this answer detailed on.
The gist of what I've done is that I've created a new class which serves the purpose of "hosting" the ObservableList from which my two windows/stages then read their data.
As the observable list automatically notifies my tableView of any changes to it, by introducing a change to it (such as what in my case is adding a new entry) with one window/stage, the other window automatically and right away has the change displayed in its tableView.
This is the class that I've created which is what I needed
public class LectureHallData{
DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();
public ObservableList<LectureHall> getLectureHallList() {
return lectureHallList;
}
public ObservableList<LectureHall> lectureHallList = FXCollections.observableArrayList(lectureHall -> new Observable[]{lectureHall.codeProperty(), lectureHall.capacityProperty()});
private ObjectProperty<LectureHall> currentLectureHall = new SimpleObjectProperty<>();
public ObservableList<LectureHall> loadLectureHalls() throws SQLException {
ObservableList<LectureHall> lectureHallObservableList = FXCollections.observableArrayList(dbcomm.queryLectureHalls());
this.lectureHallList = lectureHallObservableList;
return lectureHallObservableList;
}
public void refreshLectureHalls() throws SQLException {
this.lectureHallList.clear();
this.lectureHallList.addAll(FXCollections.observableArrayList(dbcomm.queryLectureHalls()));
}
public void addLectureHall(String hallCode, int hallCapacity) throws SQLException {
dbcomm.addLectureHall(hallCode, hallCapacity);
refreshLectureHalls();
}
}
This is the controller of the window from which the 2nd window is spawned
public class LectureHallsTabController {
@FXML
private TableView<LectureHall> lectureHallsTableView;
@FXML
private Button addNewLectureHallButton;
@FXML
private Button deleteLectureHallButton;
private TableColumn<LectureHall, String> column1;
private TableColumn<LectureHall, Integer> column2;
ArrayList<LectureHall> lhlist = new ArrayList<>();
DatabaseCommunicaton dbcomm = new DatabaseCommunicaton();
private LectureHallData data;
public void initialize() throws SQLException {
initData();
}
public void openLectureHallInputWindow(ActionEvent actionEvent) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("lectureHallInput.fxml"));
Stage stage = new Stage();
Parent root = null;
try {
root = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
LectureHallInputController lectureHallInputController = loader.getController();
lectureHallInputController.initData(data);
stage.setTitle("Lecture Input");
stage.setScene(new Scene(root));
stage.setResizable(false);
// Serves the purpose of the new window being imposed over the other window
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}
public void initData() throws SQLException {
// ensure data is only set once:
if (this.data != null) {
throw new IllegalStateException("Data can only be initialized once");
}
this.data=new LectureHallData();
column1 = new TableColumn<>("Hall code");
column2 = new TableColumn<>("Hall capacity");
column1.setCellValueFactory(new PropertyValueFactory<>("hallCode"));
column2.setCellValueFactory(new PropertyValueFactory<>("capacity"));
lectureHallsTableView.getColumns().add(0, column1);
lectureHallsTableView.getColumns().add(1, column2);
lectureHallsTableView.getItems().clear();
data.loadLectureHalls();
lectureHallsTableView.setItems(data.getLectureHallList());
}
}
This is the 2nd window's controller
public class LectureHallInputController {
@FXML
private Button confirmButton;
...
private LectureHallData data;
public void initialize() {
...
confirmButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
String hallCode = hallCodeField.getText();
int hallCapacity = Integer.parseInt(hallCapacityField.getText());
try {
data.addLectureHall(hallCode, hallCapacity);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
Stage stage = (Stage) confirmButton.getScene().getWindow();
stage.close();
}
});
}
public void initData(LectureHallData data) {
this.data = data;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…