简体   繁体   English

JavaFX从主类调用控制器方法

[英]JavaFX call a controller method from the main class

I have a little problem with my JavaFX Application, I want to call a method in a controller from the main Class but it does not work. 我的JavaFX应用程序有点问题,我想从主类中调用控制器中的方法,但它不起作用。

I tried this Accessing FXML controller class and this How do I access a UI element from another controller class in JavaFX? 我尝试了这个Accessing FXML控制器类,以及如何从JavaFX中的另一个控制器类访问UI元素?

But it does not work. 但它不起作用。

So, in my Application I have a main window and from there I can open a second window, when I close the second window I want to call a method in the main controller for updating some elements.. 所以,在我的应用程序中,我有一个主窗口,从那里我可以打开第二个窗口,当我关闭第二个窗口时,我想在主控制器中调用一个方法来更新一些元素。

My main class have this two Windows: 我的主要课程有这两个Windows:

@Override
public void start(Stage primaryStage) throws IOException {
    this.primaryStage = primaryStage;
    mainWindow();

public void mainWindow() {
    try {
        FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/App.fxml"));
        Parent root = loader.load();
        AppController appController = loader.getController();
        appController.setMain(this);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void secondWindow() throws IOException {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("SecondWindow.fxml"));
        Parent root = loader.load();
        Stage stage = new Stage();
        SecondWindowController secondWindowController = loader.getController();
        secondWindowController.setStage(stage);
        stage.initOwner(primaryStage);
        stage.initModality(Modality.WINDOW_MODAL);
        stage.setScene(new Scene(root));
        stage.show();
        stage.setOnCloseRequest(event -> {
            event.consume();
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setHeaderText("close?");
            alert.initOwner( stage);
            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK){

            // Here I want to call the method to update in the AppController

                stage.close();
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Is there a way to call the method there? 有没有办法在那里调用方法?

Your secondWindow() method is never called. 永远不会调用你的secondWindow()方法。

When you do call it, just pass the reference to the appController, which you already retrieved from the FXML via AppController appController = loader.getController(); 当你调用它时,只需将引用传递给appController,你已经通过AppController appController = loader.getController();从FXML中检索到了它AppController appController = loader.getController(); , to the method which creates the new window. ,到创建新窗口的方法。

Change the signature of: 更改签名:

secondWindow()

to: 至:

secondWindow(final AppController appController)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM