【问题标题】:JavaFX FileChooser.showOptionDialog(stage) get the Stage from an other classJavaFX FileChooser.showOptionDialog(stage) 从其他类获取舞台
【发布时间】:2016-05-14 20:45:24
【问题描述】:

我第一次在 JavaFX 项目上工作,这是我的问题:

我有一个 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();
}

我的问题是,我怎样才能获得舞台?因为 Stage 位于 MainAppController 类中。有没有什么流行的战争来获得阶段和主要阶段?

thx 4 阅读。

【问题讨论】:

    标签: java javafx-8


    【解决方案1】:

    您可以通过以下方式获得对Stage 的引用:

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

    节点可能在哪里,例如你的按钮之一。

    另一种可能性是在OptionsController 中设置Stage

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

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 2012-10-12
      • 2013-05-14
      • 2018-09-06
      相关资源
      最近更新 更多