如何在JavaFX中的窗口之间传递对象

时间:2016-08-26 05:43:32

标签: java javafx fxml

我是java新手,需要帮助理解对象。我试图在javaFx中创建一个应用程序但是在窗口之间传递对象时遇到了麻烦。目前,有一个登录窗口,如果通过检查数据库登录详细信息是正确的,则仪表板窗口将打开。但是,我现在需要在仪表板窗口中按下登录按钮时创建的BackendInterface对象,以便从BackendInterface类调用其方法。我现在有一个空指针异常。

将构造函数添加到通过LoginController的{​​{1}}类中会产生fxml加载异常

所以我的问题是如何将实例化对象传递给另一个窗口以使用它的方法?

主要

this.backendInterface

用于登录按钮的LoginController

    public class MainGui extends Application {

        @Override
        public void start(Stage primaryStage) throws Exception {

            Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
            primaryStage.setTitle("Login");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();

        }


        public static void main(String[] args) {
            launch(args);
        }
    }

仪表板上按钮的DashboardController

    public class LoginController {

        BackendInterface backendInterface;

        @FXML
        TextField username;

        @FXML
        PasswordField password;

        @FXML
        Button loginButton;

        @FXML
        Label loginLabel;

        @FXML
        public void loginButtonPress(){

            if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {

                loginLabel.setText("Please enter data in the fields below");

            } else {

                //initialises backend interface with username and password
                backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());

                //opens a connection to the database
                backendInterface.openConnection();

                if (backendInterface.getConnectionResponse() == "success"){

                    //return and print response
                    System.out.println(backendInterface.getConnectionResponse());

                    //directs the user to the dashboard after successful login
                    try{

                        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
                        Parent root1 = (Parent) fxmlLoader.load();
                        Stage stage = new Stage();
                        stage.initModality(Modality.APPLICATION_MODAL);
                        stage.setTitle("Welcome " + username.getText());
                        stage.setScene(new Scene(root1));
                        stage.show();

                        //Closes the login screen window
                        Stage stage2 = (Stage) loginButton.getScene().getWindow();
                        stage2.hide();

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

                } else {
                    //warns user of invalid login
                    loginLabel.setText(backendInterface.getConnectionResponse());
                }
            }
        }
    }

基于Clayns响应的更新解决方案:

第一个控制器加载新页面并传递对象

    public class DashboardController {

        @FXML
        public void doStuff(){

            LoginController loginController = new LoginController();

            loginController.backendInterface.printSomething();


        }

    }

第二个Controller检索对象方法

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("dashboard.fxml"));
loader.load();
Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));

DashboardController dashboardController = loader.getController();
dashboardController.setBackendInterface(backendInterface);

1 个答案:

答案 0 :(得分:0)

您在LoginController中创建的新DashboardController与用于登录的那个fxmlLoader.getController()无关。

您应该为DashboardController提供一个设置BackendInterface的方法,而不是在LoginController中使用$(document).ready(function(){ $(".click_me").click(function(){ $('.click_me').css('background-image', 'url(https://placekitten.com/500/500)'); }); });来获取DashboardController并将BackendInterface传递给它。

编辑: fabian的答案以同样的方式运作