0

So I'm using javaFX for a school project and I'm trying to change scene in my primary window but I HAVE to use the MVP model so I have to set an event handler to changing the scene inside of the presenter class. I tried doing so by using a variable from my Main class (where I initialize the first scene) and changing the scene from there

public class Main extends Application {
    public Stage window = new Stage();
    @Override
    public void start(Stage primaryStage){
        this.window = primaryStage;
        Model model = new Model();
        mainMenuView view = new mainMenuView();
        Presenter presenter = new Presenter(model, view);
        Scene mainMenu = new Scene(view);

        window.setScene(mainMenu);
        window.setTitle("Landen Quiz Main Menu");
        window.setWidth(250);
        window.setHeight(400);
        window.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}
//Main class used to make the main menu (first scene)

public class Presenter {
    private Model model;
    private view.mainMenuView mainMenuView;
    public Presenter(Model model, mainMenuView mainMenuView) {
        this.model = model;
        this.mainMenuView = mainMenuView;
        addEventHandlers();
        updateView();
    }

    private void addEventHandlers(){
        mainMenuView.getBtnHelp().setOnAction(e -> {
            helpView view = new helpView();
            Scene help = new Scene(view);
            Main.window.setScene(help);
            Main.window.setTitle("Help");
            Main.window.show();
        });

    }
    private void updateView(){
        //fill mainMenuView with data from model
    }
}
//Presenter class used to handel events in GUI

The code isn't fine tuned yet so don't judge. The button works etc but the given error is on the Main.window... in the Presenter "cannot resolve symbol 'Main'"

The presenter is packaged inside a package called 'view' and the Main class is not packaged at all, the code seems to work fine if I take the Presenter out of the package but then it's not correct according to the MVP model.

1 Answers1

0

For your error: "cannot resolve symbol 'Main'"

This errormessage is quite self-explanatory. The compiler can't figure out what "Main" is.

The first thing you got wrong is:

public class Main extends Application {
    public Stage window = new Stage();

If you want to access the window property via "Main.window" you need to make it static.

public class Main extends Application {
    public static Stage window;

And in your start you also need to change(consequent/consistent) the "this.window" to "Main.window":

public void start(Stage primaryStage){
    //this.window = primaryStage;
    Main.window = primaryStage;

The second thing is that the compiler wasn't told what Main is. Usually that is a missing import. So what you need to do is to add an import statement to your Presenter right at the beginning after the package name.

package view.presenter;
import package.Main;

But let your IDE help you - all major IDE's (Eclipse, Intellij, Netbeans...) will suggest to import the package if available.

And yes, for that you do need a package for Main. Doesn't matter how you call it - you need a package. See also: How to access java-classes in the default-package? and How to import a class from default package

As it is a Main and I assume you have to separate "view" from Main I would recommend to create a "main" package(maybe better use a project name) and a subpackage "main.view" for your Presenter. With that you'd end up with a directory structure like this:

main+
    +- view + 
    |       +- Presenter.java
    |
    +-Main.java

But ... You only need that import if the classes are in different packages. So if you put both classes into "view", you wouldn't need the import.

FWIW:

You may also get the error if your file is not named correctly. It has to be named Main.java".

If Main could not be compiled for other errors in it, that may also lead to the problem.

kai
  • 894
  • 1
  • 7
  • 15
  • Thank you for you're help, but the same error still accurs. The Main class isn't packaged at all so I can't "import Main" however if I add the 'window' to the constructor of the presenter I can use it in the presenter like that. – Justin Van Beek Mar 02 '19 at 20:38
  • You need a package. Can't help it. Create a directory and put it there. See also in the answer. – kai Mar 02 '19 at 20:40