1

I have the following method in the Controller class:

public void popup() {
    popupPane.setVisible(true);
}

If I call this method from ANOTHER class then I get a null error, I assume because I'm creating a new Controller instance that doesn't have my pane, or a new popup instance that doesn't have my controller. Anyways, is there any way to do this? I'm starting to think it's not a technical possibility within Java.

TLDR: Make a pane visible using a class outside Controller - possible?

Apple
  • 63
  • 6

1 Answers1

-2

A simple way to do this would be to get the instance of the controller that javafx created. You can do this by storing a instance of the controller as a static variable in the controllers constructor and then accessing the controllers methods thorugh that static variable.

So make your controller class like this:

     public class MyController {
     //this variable holds an instance of the class
     public static MyController mInstance;

     public MyController(){
     //in the controller set the mInstance variable to the current instance
     mInstance = this;
     }
      }

Now in the other class that you want to access a method, call it on the mInstance variable like this MyController.mInstance.someMethod(); This will ensure that the method is called on the instance of the controller that javafx created.

Please note that you should check to insure that the mInstance variable is not null before accessing.

SteelToe
  • 2,477
  • 1
  • 17
  • 28