我有以下代码:

package pl.javastart.youtufy.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

public class ConnectionErrorController implements Initializable {

    @FXML
    private Label infoLabel;

    @FXML
    private Button tryButton;

    @FXML
    private Button exitButton;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        MainController mc = new MainController();

        infoLabel.setText("Connection lost, please try again");
        tryButton.setText("try again");
        exitButton.setText("exit");

        tryButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                WebEngine webEngine = mc.getContentPaneController().getVideoWebView().getEngine(); // 1
                ToggleButton playButton = mc.getControlPaneController().getPlayButton(); // 2
                Node source = (Node) event.getSource();
                Stage stage = (Stage) source.getScene().getWindow();
                if (mc.testInet()) {
                    stage.close();
                    mc.play(webEngine, playButton);
                } else {
                    stage.close();
                    MainController.exist = false;
                }
            }
        });

        exitButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });
    }

}


我正在尝试从MainController中的控制器使用WebEngine和ToggleButton对象(我​​在MainController中为它们生成了getter和setter):

public class MainController implements Initializable {

@FXML
private ContentPaneController contentPaneController;

@FXML
private ControlPaneController controlPaneController;

@FXML
private MenuPaneController menuPaneController;

@FXML
private SearchPaneController searchPaneController;

private Youtube youtubeInstance;

public static boolean exist = false;

public ControlPaneController getControlPaneController() {
    return controlPaneController;
}

public void setControlPaneController(ControlPaneController controlPaneController) {
    this.controlPaneController = controlPaneController;
}

public ContentPaneController getContentPaneController() {
    return contentPaneController;
}

public void setContentPaneController(ContentPaneController contentPaneController) {
    this.contentPaneController = contentPaneController;
}


但是它仍然返回NullPointerException。当我尝试仅对ConnectionErrorController中的控制器进行引用时,我遇到了同样的问题。如何从ConnectionErrorController中的控制器正确引用ToggleButton i WebEngine对象?
问候

最佳答案

您正在使用“手动”创建控制器实例

MainController mc = new MainController();


@FXML带注释的字段由FXMLLoader在为您创建控制器(作为加载FXML文件的过程的一部分)时初始化。由于您创建的控制器不是FXMLLoader创建的控制器实例,因此其@FXML注释字段未初始化(即它们为null),因此会出现空指针异常。

在调用FXMLLoader之后,通过在getController()实例上调用FXMLLoader可以获得对load()创建的控制器的引用。

如果要一个控制器与另一个控制器通信,则通过在第二个控制器中定义适当的方法,将对一个控制器的引用传递给另一个控制器:

public class ConnectionErrorController implements Initializable {

    private MainController mainController ;

    public void setMainController(MainController mainController) {
        this.mainController = mainController ;
    }

    // ...

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        infoLabel.setText("Connection lost, please try again");
        tryButton.setText("try again");
        exitButton.setText("exit");

        tryButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                WebEngine webEngine = mainController.getContentPaneController().getVideoWebView().getEngine(); // 1
                ToggleButton playButton = mainController.getControlPaneController().getPlayButton(); // 2
                if (mainController.testInet()) {
                    mainController.play(webEngine, playButton);
                } else {
                    // obviously you can now do something better than the "public static field hack" here:
                    MainController.exist = false;
                }
                tryButton.getScene().getWindow().hide();
            }
        });

        // ...
    }
}


假设您正在MainController中的方法中加载第二个fxml文件,则可以执行以下操作:

public class MainController {

    // ...

    public void showConnectionErrorWindow(String fileName) {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/ConnectionError.fxml"));
        Parent root = loader.load();
        ConnectionErrorController connectionErrorController = loader.getController();
        connectionErrorController.setMainController(this);

        Scene scene = new Scene(root);
        Stage stage = new Stage();
        // etc...
    }

    // ...
}


请注意,有很多更优雅的方法可以解决此问题,例如,将ConnectionErrorController传递给回调函数(以lambda表达式的形式)以处理对play(...)的调用,这可以避免ConnectionErrorController之间的紧密耦合。 >和MainController。但是,由于您似乎不熟悉Java,因此这种更简单的方法可能更适合。

有关将值传递给控制器​​的更多信息,请参见Passing Parameters JavaFX FXML

关于java - javafx在适当的Controller类中使用MainController或其他Controller中的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43710971/

10-10 22:43