一般的な方法を作成して特定のダイアログを作成したいと思います。Javafxでジェネリックダイアログメソッドを作成
private void setDialog(String dialog,String title){
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/view/" + dialog + ".fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle(title);
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(Main.getPs());
Scene scene = new Scene(page);
dialogStage.setScene(scene);
loader.getController().setDialogStage(dialogStage);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
しかし、私はこのライン
loader.getController().setDialogStage(dialogStage)
でエラーが出ますが、正確に間違いは、私はそれをどのように修正すればよい。この
"The method setDialogStage(Stage) is undefined for the type Object"
のですか?ありがとうございました。
私はあまり経験がありません。それは、あなたが
loader.<MyController>getController().setDialogStage(dialogStage);
を行うことができます。これは本当に簡単キャストよりも、それ以上タイプセーフではありません