In JavaFX, the lifecycle of an Application
is quite different than Swing. Your constructor must take no arguments so that the Application
class can create an instance of your object. Once the instance has been created, it calls start(Stage)
, which gives you a Stage
for you to put your user interface on. This may be fine for some people, but I want to access instance variables of my Application
class (which happens to be called LJGM
), and the only way I thought I could do this was to have a single, static instance of my LJGM
object, and have a static instance()
method which returns the instance. Whenever the constructor is called, instance
is set to this
. There has to be a better way to do this.
public class LJGM extends Application {
/** The instance of {@link LJGM}. */
private static LJGM instance;
/**
* Gets the only instance of LJGM.
*
* @return The instance of LJGM.
*/
public static LJGM instance() {
return instance;
}
// (other variables here)
/**
* Instantiates a new LJGM object.
*/
public LJGM() {
LJGM.instance = this;
// (initialize other variables)
}
/*
* (non-Javadoc)
*
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start(Stage primaryStage) throws Exception {
// (add GUI to the primaryStage here)
}
/**
* Main method.
*
* @param args
* The arguments to pass to the application.
*/
public static void main(String[] args) {
launch(args);
}
}
Here is the Application Javadoc in case you need it.