表未在setOnCloseRequest上刷新

时间:2017-11-15 14:56:15

标签: java javafx

stageAdd窗口更新工作人员并按下按钮ok时,refreshButtonClicked()上的stage.setOnCloseRequest((e)-> refreshButtonClicked());方法未执行。更新完成并单击按钮确定后,应自动刷新workersTable

我在stageAdd方法中按下编辑按钮时使用editButtonClicked()

 public class ElectricalDepartmentController {

    @FXML private Label name;
    @FXML private Label surname;
    @FXML private Label age;
    @FXML private Label city;
    @FXML private Label address;
    @FXML private Label telephoneNum;
    @FXML private Label email;
    @FXML private Label idNumber;
    @FXML private Label startDate;
    @FXML private Label contractType;
    @FXML private Label endDate;
    @FXML private Label payFrequency;
    @FXML private Label accountNumber;
    @FXML private Label taxCoefficient;
    @FXML private Label netSalary;
    @FXML private Button refreshButton;
    @FXML private Button deleteButton;
    @FXML private Button editButton;
    @FXML private TableView<Worker> workersTable;
    @FXML private TableColumn<Worker, String> workersTableColumn;

    private WorkerDao workerDao = WorkerDaoFactory.getWorkerDao();
    private final ObservableList<Worker> workersList = FXCollections.observableArrayList();

    //initialize method
    public void initialize() throws IOException {
        workersTableColumn.setCellValueFactory(new PropertyValueFactory<>("nameSurname"));
        rowSelected();
    }

    //Refresh the table when the button is clicked
    @FXML
    public void refreshButtonClicked() {
        workersList.removeAll(workersList);
        populateTable();
    }

    //Delete worker
    @FXML
    private void deleteButtonClicked() {
        Worker selectedWorker = workersTable.getSelectionModel().getSelectedItem();
        workersTable.getItems().remove(selectedWorker);
        workerDao.deleteSelectedWorker(selectedWorker, "Electrical");
    }

    //Populate the table
    @FXML
    public void populateTable() {
        for(Worker worker : workerDao.getWorkersNameSurname("Electrical")) workersList.addAll(worker);
        workersTable.setItems(workersList);
    }

    //Show worker's information when row is selected
    @FXML
    private void rowSelected() {

        workersTable.getSelectionModel().selectedItemProperty().
                addListener((observable, oldValue, newValue) -> {

                    if (newValue != null) {
                        name.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getName());
                        surname.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getSurname());
                        age.setText(String.valueOf(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getAge()));
                        city.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getContactInformation().getCity());
                        address.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getContactInformation().getAddress());
                        telephoneNum.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getContactInformation().getTelephoneNum());
                        email.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getContactInformation().getEmail());
                        idNumber.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getIdNumber().toString());
                        startDate.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getStartDate().toString());
                        contractType.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getContractType());

                        if(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getEndDate()== null)endDate.setText("");
                        else endDate.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getEndDate().toString());

                        payFrequency.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getPayFreq());
                        accountNumber.setText(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getAccountNum().toString());
                        taxCoefficient.setText(String.valueOf(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getTaxCoeficient()));
                        netSalary.setText(String.valueOf(workerDao.getWorkersInfoByNameSurname(newValue.getNameSurname()).getEmploymentInformation().getNetSalary()));
                    }
                });
    }
    @FXML
    public void editButtonClicked() throws IOException {
        Worker workerSelectedForEdit = workersTable.getSelectionModel().getSelectedItem();

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/StageAdd.fxml"));
        Parent parent = loader.load();

        StageAddController controller = loader.getController();
        controller.showEditWorkerOldInformation(workerSelectedForEdit);

        Stage stage = new Stage();
        stage.setTitle("Edit Worker");
        stage.setScene(new Scene(parent));
        parent.getStylesheets().add("/css/redBorder.css");

        stage.show();

        stage.setOnCloseRequest((e)-> refreshButtonClicked());

    }
}

关闭stageAdd并在stageAdd控制器类中保存/更新工作程序的方法:

 //Close Stage
    @FXML private void closeButtonClicked(){
        Stage stage = (Stage) rootPane.getScene().getWindow();
        stage.close();
    }
//Submit entered information - OK button pressed
    @FXML private void okButtonClicked() throws IOException{
        WorkerDao workerDao = WorkerDaoFactory.getWorkerDao();
        List<Control> requiredFieleds = new ArrayList<>();
        int count=0;

        // Getting required fields
        for(Control c : allControls){
            if(!c.equals(email) && !c.equals(endDate)){
                requiredFieleds.add(c);
            }
        }

        //Check if all required fields are valid
        for (Control c : requiredFieleds) {
            if (c instanceof TextField) {
                if (!stageAddService.validateField((TextField)c, field-> field.getText().trim().isEmpty()))
                    count++;
                else c.pseudoClassStateChanged(errorClass, true);
            }
            else if (c instanceof DatePicker){
                if (!stageAddService.validateField((DatePicker)c, field-> field.getValue()==null))
                    count++;
                else c.pseudoClassStateChanged(errorClass1, true);
            }
            else {
                if (!stageAddService.validateField((ChoiceBox)c, field-> field.getValue()==null))
                    count++;
                else c.pseudoClassStateChanged(errorClass2, true);
            }
        }

        //If required fields are valid, save data
        if (count == requiredFieleds.size()) {
            Worker worker = new Worker();

            worker.setName(name.getText().trim());
            worker.setSurname(surname.getText().trim());
            worker.setAge(age.getValue());

            ContactInformation contactInformation = new ContactInformation();
            contactInformation.setAddress(address.getText().trim());
            contactInformation.setCity(city.getText().trim());
            contactInformation.setTelephoneNum(telephoneNumber.getText().trim());
            contactInformation.setEmail(email.getText().trim());
            worker.setContactInformation(contactInformation);

            EmploymentInformation employmentInformation = new EmploymentInformation();
            employmentInformation.setDepartment(departmentBox.getValue());
            employmentInformation.setIdNumber(Long.parseLong(idNumber.getText().trim()));
            employmentInformation.setStartDate(startDate.getValue());
            employmentInformation.setContractType(contractType.getValue());
            employmentInformation.setEndDate(endDate.getValue());
            employmentInformation.setPayFreq(payFrequency.getValue());
            employmentInformation.setAccountNum(Long.parseLong(accountNumber.getText().trim()));
            employmentInformation.setTaxCoeficient(Double.parseDouble(taxCoeficient.getText().trim()));
            employmentInformation.setNetSalary(Double.parseDouble(netSalary.getText().trim()));
            worker.setEmploymentInformation(employmentInformation);

            if(isInEditMode){
                worker.setId(id);
                workerDao.updateWorker(worker);
            }
            else {workerDao.saveWorker(worker);}

            closeButtonClicked();

        }
        else {error1.setText("Fill in all red fields");
            error2.setText("Fill in all red fields");
        }

    }

其他类中加载stageAdd

的方法
public static void showStageAdd() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/views/StageAdd.fxml"));
        BorderPane addWorkerPane = loader.load();
        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);
        stageAdd.initOwner(stage1);
        stageAdd.showAndWait();
    }

1 个答案:

答案 0 :(得分:2)

我们遇到了类似的问题 - &gt;以下代码:

stage.setOnCloseRequest((e)-> refreshButtonClicked());

当窗口通过窗口上的“X”关闭时,将调用此方法。但是这段代码

Stage stage = (Stage) rootPane.getScene().getWindow();
stage.close();

实际上不会调用关闭请求。您可以通过向窗口发送close事件而不是stage.close()来完成此操作,或者您可以执行此操作:

stage.setOnHidden((e)-> refreshButtonClicked());