Arjun Koroth
Arjun Koroth

Reputation: 25

How to switch views in Gluon mobile app using javafx?

I am trying to create a Gluon mobile application using javafx. I want to create a login page in which on a successful login i need to load another(Second View) view in a button click. I didn't get a proper example for this. If anyone knows this please help. I have two views Primary presenter and secondary presenter.(gluon application with FXML).Below is my primary view's controller.

public class PrimaryPresenter {

@FXML
private View primary;

private Label label;
@FXML
private TextField username;
@FXML
private Button loginBt;

private Alert alert;
@FXML
private PasswordField password;
public void initialize() {
    primary.showingProperty().addListener((obs, oldValue, newValue) -> {
        if (newValue) {
            AppBar appBar = MobileApplication.getInstance().getAppBar();
            appBar.setNavIcon(MaterialDesignIcon.MENU.button(e
                    -> MobileApplication.getInstance().showLayer(ArjunsApp.MENU_LAYER)));
            appBar.setTitleText("Primary");
            appBar.getActionItems().add(MaterialDesignIcon.SEARCH.button(e
                    -> System.out.println("Search")));
        }
    });
}

@FXML
private void buttonClick(ActionEvent event) {
    if(username.getText().equals("")){
        alert = new Alert(AlertType.ERROR,"Enter username");
        alert.showAndWait();
    }else if(password.getText().equals("")){
        alert = new Alert(AlertType.ERROR,"Enter password");
        alert.showAndWait();
    }else{
        //Code to load my secondary view
    }
}

}

Upvotes: 1

Views: 1713

Answers (1)

José Pereda
José Pereda

Reputation: 45456

Assuming you are using the Gluon plugin - Multi View project with FXML template, you can switch views easily with MobileApplication.getInstance().switchView(viewName).

In your case:

@FXML
private void buttonClick(ActionEvent event) {
    ...
    MobileApplication.getInstance().switchView("SECONDARY_VIEW");
}

If you are using the Glisten-Afterburner template instead (it also uses FXML), you could use something like:

@FXML
private void buttonClick(ActionEvent event) {
    ...
    AppViewManager.SECONDARY_VIEW.switchView();
}

You can find more about the Gluon Mobile API here.

Upvotes: 3

Related Questions