2017-11-05 3 views
-2

ここにtableviewコントローラがあります。 ボタンを押すと、選択した行に関連付けられたデータをInstructorProfileContollerに送信しようとしています。コードの作品や版画は、あなたはメソッド呼び出しstage.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(); 
     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

上で任意のデータを送信しませんし、「あなたはインストラクターを選択し、」しかし。したがって、ユーザーがウィンドウを閉じるまで値を設定しないでください。呼び出し前に値を設定した呼び出しをshowAndWait()に移動するだけです。

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

    // ... 
} 

、その後、もちろん、ただ

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.setInstructor(instructor); 

    // ... 
} 

の操作を行います。さておき、ちょうどあなたのInstructorProfileController方法setInstructor(...)を定義するためのより良いスタイルであるように

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

そうすれば、関連するコントローラーの外にUIコントロールが公開されることがなくなり、コードがはるかに保守的になります。

+0

ありがとうございます!私は自分のコードが間違っていると思った –

関連する問題