尝试将所选行主键发送到另一个控制器并使用该主键加载数据

时间:2017-11-05 17:47:56

标签: java javafx

这是tableview控制器: 按下按钮时,尝试将与所选行关联的数据发送到InstructorProfileContoller。代码工作并打印“您选择了一个教师”,但之后不会通过

发送任何数据
    Instructor instructor = (Instructor) InsTable.getSelectionModel().getSelectedItem();
    if(instructor != null){
        System.out.println("You select an instructor");
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("InstructorProfile.fxml"));
        Parent root = fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("update instructor");
        stage.setScene(new Scene(root));
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.showAndWait();


        InstructorProfileController instructorProfileController = fxmlLoader.getController();
        instructorProfileController.countrychoicebox.setValue(instructor.getCountryID());
        instructorProfileController.insstatuschoicebox.setValue(instructor.getIns_statusCode());
        instructorProfileController.firstnametextfield.setText(instructor.getIns_firstName());
        instructorProfileController.lastnametextfield.setText(instructor.getIns_lastName());
        instructorProfileController.addresstextfield.setText(instructor.getIns_address());
        instructorProfileController.citytextfield.setText(instructor.getIns_city());
        //  instructorProfileController.dateofBirthpicker.setValue(instructor.getIns_dateOfBirth());
        instructorProfileController.gendertextfield.setText(instructor.getIns_sex());
        instructorProfileController.zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode()));
    }
    initialize();

}

1 个答案:

答案 0 :(得分:1)

方法调用SUB显示阶段,然后等待它关闭。因此,在用户关闭窗口之前,不要设置值。只需在调用stage.showAndWait()之前将设置值的调用移动。

showAndWait()

顺便说一句,在Instructor instructor = (Instructor) InsTable.getSelectionModel().getSelectedItem(); if(instructor != null){ System.out.println("You select an instructor"); FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("InstructorProfile.fxml")); Parent root = fxmlLoader.load(); InstructorProfileController instructorProfileController = fxmlLoader.getController(); instructorProfileController.countrychoicebox.setValue(instructor.getCountryID()); instructorProfileController.insstatuschoicebox.setValue(instructor.getIns_statusCode()); instructorProfileController.firstnametextfield.setText(instructor.getIns_firstName()); instructorProfileController.lastnametextfield.setText(instructor.getIns_lastName()); instructorProfileController.addresstextfield.setText(instructor.getIns_address()); instructorProfileController.citytextfield.setText(instructor.getIns_city()); // instructorProfileController.dateofBirthpicker.setValue(instructor.getIns_dateOfBirth()); instructorProfileController.gendertextfield.setText(instructor.getIns_sex()); instructorProfileController.zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode())); Stage stage = new Stage(); stage.setTitle("update instructor"); stage.setScene(new Scene(root)); stage.initModality(Modality.APPLICATION_MODAL); stage.showAndWait(); } initialize(); 中定义方法setInstructor(...)会是一种更好的方式:

InstructorProfileController

然后,当然,只需做

public class InstructorProfileController {

    // ...

    public void setInstructor(Instructor instructor) {
       countrychoicebox.setValue(instructor.getCountryID());
       insstatuschoicebox.setValue(instructor.getIns_statusCode());
       firstnametextfield.setText(instructor.getIns_firstName());
       lastnametextfield.setText(instructor.getIns_lastName());
       addresstextfield.setText(instructor.getIns_address());
       citytextfield.setText(instructor.getIns_city());
        // dateofBirthpicker.setValue(instructor.getIns_dateOfBirth());
       gendertextfield.setText(instructor.getIns_sex());
       zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode()));
    }

    // ...
}

这样,您就不会在相关控制器之外公开UI控件,这将使您的代码更易于维护。