简体   繁体   中英

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
    }
}

}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM