#Swap scene after login from main class method

26 messages · Page 1 of 1 (latest)

sand tartan
#
public class main_gui_class extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        log_in_scene(stage);



    }
    public void log_in_scene(Stage stage) throws IOException{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("log_in_page.fxml"));
        Parent root = fxmlLoader.load();

        Scene scene = new Scene(root);

        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();


    }

    public void home_page_scene(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("home_page.fxml"));
        Parent root = fxmlLoader.load();

        Scene scene = new Scene(root);

        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
public class login_controller {
    @FXML
    private TextField username;

    @FXML
    private TextField password;

    private Stage stage;




    String user = "admin";
    String pass = "admin1";

    @FXML
    protected void loginClicked(ActionEvent event) throws IOException {

        if (username.getText().equals(user) && password.getText().equals(pass)){
            // Call it from here
            System.out.println("SUCCESS");



        }else {
            System.out.println("INVALID PASSWORD");
        }


    }
}

I want to call the home_page_scene from the login controller

broken pecanBOT
#

This post has been reserved for your question.

Hey @sand tartan! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

queen blade
#

you need an instance of main_gui_class in your controller

#

so you can add a field and a setter for it

#

and when loading the FXML, you can do

login_controller ctl = (login_controller) fxmlLoader.getController();
ctl.setMainClass(this);
#

btw class names should be PascalCase by convention

#

not snake_case

noble galleon
#

First of all make a static library for all of ur scenes:

public class SceneLoader {
  public static final String LOG_IN = "log_in_page.fxml";
  public static final String HOME_PAGE = "...";
  //it would be even better if u make an enum for these strings, since that prevents load being called with any random BS

  public static Parent load(final String scene) {
    try {
      Path resource = getClass().getResource(scene);
      if (resource == null) throw new FileNotFounException(
            "The FXML scene: \"%s\" was not found in the resource folder"
            .formatted(scene));

      return new FXMLLoader(resource).load();

    } catch (IOException e) {
      throw new IllegalStateException(
            "Unable to read the file \"%s\" when loading a scene"
            .formatted(scene),
            e);
    }
  }

this gives u the error handling u are completely missing from ur code,
deals with the Checked exception
AND removes the code duplication in ur program

#

Then you would save the stage given to u in the start method and write a method load scenes, again removing code duplication to use that stage plus whatever loaded scene given to it

#

and then u have a choice, as dan1st shows above u can set the main class in the controllers after the FXMLLoader makes them (which means u have the publically accessable set method than can mess things up by accident

or u can construct the controllers ahead of time with a link to Main

queen blade
#

i.e. with a setter (possibly in an interface or abstract class)

sand tartan
noble galleon
#

the same principles apply in python and java

sand tartan
noble galleon
#

loose coupling, single responsibility, law of demeter, no magic numbers, readibility, consistant style, etc

sand tartan
noble galleon
#

did u create a maven project?

sand tartan
#

The features are too good

noble galleon
#

then it generated the correct paths already
.... /src/java/main/resources or something ... u can put a folder in there to group them together if u like, but thats the exact correct place for support files for the project

sand tartan
#

aight

#

btw so I finally understand that the scene builder is basically just a method that makes scene instance thingly less of a struggle and confusing

noble galleon
#

yeah, r u using the new gluon one of the old oracle one?

#

the scenebuilder integrates perfectly with intelliJ, giving u code completion for controller names etc