gpt4 book ai didi

java - setOnCloseRequest 时表未刷新

转载 作者:行者123 更新时间:2023-12-02 02:29:48 24 4
gpt4 key购买 nike

当在 stageAdd 窗口中更新工作程序并按下 ok 按钮时,refreshButtonClicked() 方法不会在 上执行>stage.setOnCloseRequest((e)->refreshButtonClicked());。更新完成并单击“确定”按钮后,workersTable 应自动刷新。

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

 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 Controller 类中保存/更新worker的方法:

 //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();
}

最佳答案

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

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

当通过窗口上的“X”关闭窗口时,将调用此函数。然而这段代码

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

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

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

关于java - setOnCloseRequest 时表未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47310569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com