JavaFX的FileChooser.showOptionDialog(阶段)从其他类

问题描述:

我工作的上的JavaFX项目首次在这里得到舞台是我的问题:JavaFX的FileChooser.showOptionDialog(阶段)从其他类

我哈瓦一个MainApp,从那里我打开主窗口,我有一个MenuBar,并从MenuBar我打开一个新的窗口,这是从MainApp在控制器中调用。

public void optionWindow() throws IOException { 
    Parent root = FXMLLoader.load(getClass().getResource("/views/options.fxml")); 
    Stage stage = new Stage(); 
    stage.setTitle("options"); 
    stage.setScene(new Scene(root)); 
    stage.setResizable(false); 
    stage.setAlwaysOnTop(true); 
    stage.show(); 
} 

在这个新窗口中,我有两个按钮,其中一个应该使用OptionsController类中的方法打开一个FileChooser。

public void updateOptions() { 
    FileChooser chooser = new FileChooser(); 
    chooser.showOpenDialog(stage); 
} 

MainApp启动方法:

@Override 
public void start(Stage primaryStage) throws IOException { 
    Parent root = FXMLLoader.load(getClass().getResource("/views/Application.fxml")); 
    primaryStage.setTitle("Application"); 
    primaryStage.setScene(new Scene(root)); 
    primaryStage.show(); 
} 

我的问题是,如何才能得到舞台?因为舞台位于MainAppController类中。是否有任何流行的战争让阶段和主要舞台?

thx 4阅读。

你可以通过向Stage参考:

Stage stage = (Stage) node.getScene().getWindow()

其中节点可以是如你的其中一个按钮。

另一种可能性是设置StageOptionsController

FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/options.fxml")); 
Parent root = null; 
try { 
     root = loader.load(); 
} catch (IOException e) { 
     e.printStackTrace(); 
} 
OptionsController controller = loader.getController(); 
controller.setParentStage(stage); 
+0

THX也就迎刃而解了 – Dusius