Search code examples
javafxgluongluon-mobile

Unable to Switch Views with Button in Gluon


I tried to do follow How to switch views in Gluon mobile app using javafx?, but it is not switching my views. Could you advise me on what could be going on, or if there is something I am missing? This is not working all over the project in multiple different instances where I have tried to make a button switch from one view to the other. Here is an example:

  @Override
protected void updateAppBar(AppBar appBar) {
    appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApplication().getDrawer().open()));
    appBar.setTitleText("No Account? Create one! ->");
    appBar.getActionItems().add(MaterialDesignIcon.PERSON_ADD.button(e -> {
        System.out.println("Switch to secondary view.");
        MobileApplication.getInstance().switchView("SECONDARY_VIEW");
    }));
}

For reference, I have no other errors in my code, and all views and transitions through the multi-view sidebar are working perfectly.

Edit: Per request, here is the code where I add the SECONDARY_VIEW to the view factory.

package com.mysecondapplication;

import com.mysecondapplication.views.PrimaryView;
import com.mysecondapplication.views.SecondaryView;
import com.mysecondapplication.views.TertiaryView;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.visual.Swatch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class MySecondApplication extends MobileApplication {

public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "Secondary View";
public static final String TERTIARY_VIEW = "Tertiary View";

@Override
public void init() {
    addViewFactory(PRIMARY_VIEW, PrimaryView::new);
    addViewFactory(SECONDARY_VIEW, SecondaryView::new);
    addViewFactory(TERTIARY_VIEW, TertiaryView::new);

    DrawerManager.buildDrawer(this);
}

@Override
public void postInit(Scene scene) {
    Swatch.AMBER.assignTo(scene);

    scene.getStylesheets().add(MySecondApplication.class.getResource("style.css").toExternalForm());
    ((Stage) scene.getWindow()).getIcons().add(new Image(MySecondApplication.class.getResourceAsStream("/icon.png")));
}

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

Edit: instantiating views:

Primary View

package com.mysecondapplication.views;

import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.control.FloatingActionButton;
import com.gluonhq.charm.glisten.control.Icon;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class PrimaryView extends View {

public PrimaryView() {
    
    Label label = new Label("Hello JavaFX World!");

    Button button = new Button("Change the World!");
    button.setGraphic(new Icon(MaterialDesignIcon.LANGUAGE));
    button.setOnAction(e -> MobileApplication.getInstance().switchView("SECONDARY_VIEW"));
    
    VBox controls = new VBox(15.0, label, button);
    controls.setAlignment(Pos.CENTER);
    
    setCenter(controls);
}

@Override
protected void updateAppBar(AppBar appBar) {
    appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApplication().getDrawer().open()));
    appBar.setTitleText("No Account? Create one! ->");
    appBar.getActionItems().add(MaterialDesignIcon.PERSON_ADD.button(e -> {
        System.out.println("Switch to secondary view.");
        MobileApplication.getInstance().switchView("SECONDARY_VIEW");
    }));
}

}

Secondary View

package com.mysecondapplication.views;

import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.control.FloatingActionButton;
import com.gluonhq.charm.glisten.control.Icon;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class SecondaryView extends View {

public SecondaryView() {
    
    Label label = new Label("Hello JavaFX World!");

    Button button = new Button("Change the World!");
    button.setGraphic(new Icon(MaterialDesignIcon.LANGUAGE));
    button.setOnAction(e -> System.out.println("Hello!"));
    
    VBox controls = new VBox(15.0, label, button);
    controls.setAlignment(Pos.CENTER);
    
    setCenter(controls);
    
}

@Override
protected void updateAppBar(AppBar appBar) {
    appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApplication().getDrawer().open()));
    appBar.setTitleText("Have an Account? Log In! ->");
    appBar.getActionItems().add(MaterialDesignIcon.PEOPLE.button(e -> { 
        System.out.println("Switch to Primary View.");
        MobileApplication.getInstance().switchView("PRIMARY_VIEW");
    }));
}
}

Solution

  • When switching a view, you're passing a string which does not much the one you used when registering it. To make sure it matches, use the constants already defined in your MySecondApplication class