Departments controllers are loaded from SceneWithButtonsController
. StageAddController
is loaded from LayoutWithHomeAndAddController
. I want to execute this method from StageAddController
which executes corresponding method from departments controller :
//Show Stage to add a worker
public class LayoutWithHomeAndAddController {
@FXML
private void addButtonClicked() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/views/StageAdd.fxml"));
BorderPane addWorkerPane = loader.load();
StageAddController stageAddController = loader.getController();
Stage stageAdd = new Stage();
stageAdd.setTitle("Add New Worker");
stageAdd.resizableProperty().setValue(Boolean.FALSE);
Scene scene = new Scene(addWorkerPane);
scene.getStylesheets().add("/css/redBorder.css");
stageAdd.setScene(scene);
stageAdd.initModality(Modality.WINDOW_MODAL);
Stage mainStage = (Stage) root.getScene().getWindow();
stageAdd.initOwner(mainStage);
stageAdd.show();}
}
Controller class where departments are loaded:
public class SceneWithButtonsController {
//Show workers from Mechanical Department
@FXML
private void mechanicalDepartmentButtonClicked() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/views/MechanicalDepartment.fxml"));
BorderPane mehOdjel = loader.load();
MechanicalDepartmentController mechanicalDepartmentController = loader.getController();
mechanicalDepartmentController.populateTable();
BorderPane borderPane = (BorderPane) pane.getParent();
borderPane.setCenter(mehOdjel);
}
//Show workers from Electrical Department
@FXML
private void electricalDepartmentButtonClicked() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/views/ElectricalDepartment.fxml"));
BorderPane elOdjel = loader.load();
ElectricalDepartmentController electricalDepartmentController = loader.getController();
electricalDepartmentController.populateTable();
BorderPane borderPane = (BorderPane) pane.getParent();
borderPane.setCenter(elOdjel);
}
}
StageAddController:
public class StageAddController {
private ElectricalDepartmentController electricalDepartmentController;
private MechanicalDepartmentController mechanicalDepartmentController;
public void setElectricalDepartmentController(ElectricalDepartmentController electricalDepartmentController) {
this.electricalDepartmentController = electricalDepartmentController;
}
public void setMechanicalDepartmentController(MechanicalDepartmentController mechanicalDepartmentController) {
this.mechanicalDepartmentController = mechanicalDepartmentController;
}
public void refreshTableOnSaveButtonClicked() {
if (departmentBox.getValue().equals("Electrical")) {
electricalDepartmentController.refreshButtonClicked();
}
else if(departmentBox.getValue().equals("Mechanical")) {
mechanicalDepartmentController.refreshButtonClicked();
}
}
}
This means that refreshButtonClicked()
method from ElectricalDepartmentController
or MechanicalDepartmentController
will be executed.
ElectricalDepartmentController (same is with MechanicalDepartmentController):
public class ElectricalDepartmentController {
public void initialize() throws IOException {
StageAddController stageAddController = new StageAddController();
stageAddController.setElectricalDepartmentController(this);
workersTableColumn.setCellValueFactory(new PropertyValueFactory<>("nameSurname"));
rowSelected();
}
@FXML
public void populateTable() {
for(Worker worker : workerDao.getWorkersNameSurname("Electrical")) workersList.addAll(worker);
workersTable.setItems(workersList);
}
@FXML
public void refreshButtonClicked() {
workersList.removeAll(workersList);
populateTable();
}
}
How to achieve this.