Rectangle 27 0

java Pass value between two JavaFX windows that are setup through fxml?


public class AuthenticationState {

    private final BooleanProperty loggedIn = new SimpleBooleanProperty(false);

    public BooleanProperty loggedInProperty() {
        return loggedIn ;
    }

    public final boolean isLoggedIn() {
        return loggedInProperty().get();
    }

    public final void setLoggedIn(boolean loggedIn) {
        loggedInProperty().set(loggedIn);
    }

    private final StringProperty userName = new SimpleStringProperty();

    public StringProperty userNameProperty() {
        return userName ;
    }

    public final String getUserName() {
        return userNameProperty().get();
    }

    public final void setUserName(String userName) {
        userNameProperty().set(userName);
    }

    // other properties as needed, e.g. IntegerProperty logInAttempts , etc.
}
public class LoginController {

    private AuthenticationState authenticationState ;

    public void setAuthenticationState(AuthenticationState authenticationState) {
        this.authenticationState = authenticationState ;
    }

    @FXML
    public void login() {
        // check login:
        boolean loginSuccessful = ... ;

        authenticationState.setLoggedIn(loginSuccessful);

        // ...
    }
}
public class MainController {

    @FXML
    private final MenuItem deleteAllDataMenuItem ;

    private AuthenticationState authenticationState ;

    public void initialize() {

        authenticationState = new AuthenticationState();

        deleteAllDataMenuItem.disableProperty()
            .bind(authenticationState.loggedInProperty().not());

    }

    @FXML
    public void logIn() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LoginForm.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();

            LoginController loginController = fxmlLoader.getController();
            loginController.setAuthenticationState(authenticationState);

            Stage stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.setTitle("HWI - Login");
            stage.setResizable(false);
            stage.setScene(new Scene(root1));
            stage.showAndWait();

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

JavaFX makes this particularly easy by implementing observable properties for you, which just wrap values and make it easy to add listeners that are notified if they change.

Now when the user logs in, the login controller sets the loggedIn property in the authenticationState to true. Since the disabled state of the menu item is bound to (the negative of) the loggedIn property, the menu item is automatically enabled. If you have a "Log Out" button, just have it set the loggedIn property to false, and the menu item will be disabled again.

So in this example you might do something like:

The generally accepted way to share data between two or more distinct parts of the UI is to use a MVC/MVP type approach. In these patterns, you encapsulate the data in a "model" (the "M" in either) in a way that it can be observed and notifications sent if it changes. Then one part of the UI can update the model, and other parts of the UI that are observing it will be notified if it changes.

Note