如何将User对象从一个Controller传递到另一个JavaFX Controller?

时间:2017-11-02 06:19:46

标签: java javafx scene

很难解释,但我会尝试。我在场景#1控制器中有一个User对象,我希望将此用户传递给场景#2控制器。 这是第一个控制器:

            @FXML
            private TextField password;
            @FXML
            private TextField username;
            private String id,pass;
            private User loginUser;

            DisplayController controller = new DisplayController();
            id = username.getText();
            pass = password.getText();
            loginUser = databaseStack.checkUser(id,pass);
            controller.passUser(loginUser);
            System.out.println(loginUser);
            Parent root = null;
            try {
                root = FXMLLoader.load(Main.class.getResource("javafx3.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene signUpScene = new Scene(root);
            Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(signUpScene);
            window.show();

* databaseStack.checkUser()是检查有效用户的不同类中的方法; 这是第二个控制器:

@FXML
private Button findFriend;
@FXML
private Label fullName;
@FXML
private Label eMail;
@FXML
private Label hobbies;
@FXML
private Label major;
private User loginUser;
public void initialize(){
    System.out.println(loginUser);
    findFriend.setStyle("-fx-background-color: \n" +
            "        linear-gradient(#ffd65b, #e68400),\n" +
            "        linear-gradient(#ffef84, #f2ba44),\n" +
            "        linear-gradient(#ffea6a, #efaa22),\n" +
            "        linear-gradient(#ffe657 0%, #f8c202 50%, #eea10b 100%),\n" +
            "        linear-gradient(from 0% 0% to 15% 50%, rgba(255,255,255,0.9), rgba(255,255,255,0));\n" +
            "    -fx-background-radius: 30;\n" +
            "    -fx-background-insets: 0,1,2,3,0;\n" +
            "    -fx-text-fill: #654b00;\n" +
            "    -fx-font-weight: bold;\n" +
            "    -fx-font-size: 20px;\n" +
            "    -fx-padding: 10 20 10 20;");
    findFriend.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            Parent root = null;
            try {
                root = FXMLLoader.load(Main.class.getResource("javafx4.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene signUpScene = new Scene(root);
            Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(signUpScene);
            window.show();
        }
    });
}
public void passUser(User user){
    loginUser = user;
}

在控制器1中,我将用户“loginUser”传递给控制器​​2中的passUser()方法。在方法passUser()的控制器2中,我将私有变量loginUser设置为方法已接收的用户。然后我在initialize()中打印出这个User对象。但是控制器2中的initialize()打印出“null”而不是我在passUser()中传递的User。它可能发生,因为当我切换场景时,它会重置所有变量。我如何解决它?如何在passUser()方法中收到传递给局部变量的User对象? 对不起,解释不好,我很难解释这个问题。 感谢您的时间和精力!

1 个答案:

答案 0 :(得分:1)

这是控制器之间直接通信的一个例子。

public class ControllerA {
    @FXML
    private TextField usernameTextField;

    @FXML
    private Button loginButton;

    private Stage stage;

    @FXML
    private void initialize() {
        loginButton.setOnAction(event -> {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("scene_2.fxml"));
                Parent parent = loader.load();

                ControllerB controllerB = loader.getController();
                controllerB.setUser(new User(usernameTextField.getText()));

                stage.setScene(new Scene(parent));

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

    public void setStage(Stage stage) {
        this.stage = stage;
    }
}

public class ControllerB {

    private User user;

    @FXML
    private Label wellcomeLabel;

    @FXML
    private void initialize() {

    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        wellcomeLabel.setText("Wellcome " + user.getName());
    }
}

public class MainStage extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("scene_1.fxml"));
        Parent parent = loader.load();

        ControllerA controllerA = loader.getController();
        controllerA.setStage(primaryStage);

        primaryStage.setTitle("Demo");
        primaryStage.setScene(new Scene(parent));
        primaryStage.show();
    }

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

如果要使用调解员,事情会是这样的:

public class Mediator {
    private static Mediator INSTANCE;

    private Stage stage;
    private User user;

    public static Mediator getInstance() {
        if(INSTANCE == null) {
            INSTANCE = new Mediator();
        }
        return INSTANCE;
    }

    private Mediator() {
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

public class MainStage extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Mediator.getInstance().setStage(primaryStage);

        Parent parent = FXMLLoader.load(getClass().getResource("scene_1.fxml"));

        primaryStage.setTitle("Demo");
        primaryStage.setScene(new Scene(parent));
        primaryStage.show();
    }

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

public class ControllerA {
    @FXML
    private TextField usernameTextField;

    @FXML
    private Button loginButton;

    @FXML
    private void initialize() {
        loginButton.setOnAction(event -> {
            try {
                Mediator.getInstance().setUser(new User(usernameTextField.getText()));
                Parent parent = FXMLLoader.load(getClass().getResource("scene_2.fxml"));

                Stage stage = Mediator.getInstance().getStage();
                stage.setScene(new Scene(parent));

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

public class ControllerB {

    @FXML
    private Label wellcomeLabel;

    @FXML
    private void initialize() {
        User user = Mediator.getInstance().getUser();
        wellcomeLabel.setText("Wellcome " + user.getName());
    }

}

FXML文件与我没有给他们的代码无关。

当然是课程用户

public class User {
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}