Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting JavaFX from Main method of class which doesn't extend Application

I'm having problem to start a JavaFX Application from a Main method of a class which doesn't extend javafx.application.Application

In my application there is the MainApp.java which should start the overriden method start() in the MainUIController.java, which extends Applciation

When I start the Main method from the MainUIController.java everything works fine.

MainApp.java

public class MainApp {

    public static void main(String[] args) {
        PersonJDBCTemplate jdbc = connect();
        MainUIController mUIc = new MainUIController(jdbc);
        mUIc.start(new Stage());

    }

    public static PersonJDBCTemplate connect() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        PersonJDBCTemplate personJDBCTemplate = (PersonJDBCTemplate) context
                .getBean("personJDBCTemplate");
        return personJDBCTemplate;
    }
}

MainUIController.java

public class MainUIController extends Application {

    private Stage stage;
    // private User loggedUser;
    private final double MINIMUM_WINDOW_WIDTH = 800.0;
    private final double MINIMUM_WINDOW_HEIGHT = 570.0;
    private String version = "0.6";
    private PersonJDBCTemplate jdbc;

    public MainUIController(PersonJDBCTemplate jdbc) {
        this.jdbc = jdbc;

    }

    @Override
    public void start(Stage primaryStage) {
        try {
            stage = primaryStage;
            stage.setTitle("Sharp");
            stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
            stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
            stage.setResizable(false);
            gotoLogin();
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(MainUIController.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
    }

    public void gotoLogin() {
        try {
            LoginController login = (LoginController) replaceSceneContent("/fxml/Login.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(MainUIController.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
    }
}

After running the MainApp, I get the following Error :

Exception in thread "main" java.lang.ExceptionInInitializerError
at javafx.stage.Window.<init>(Window.java:1110)
at javafx.stage.Stage.<init>(Stage.java:236)
at javafx.stage.Stage.<init>(Stage.java:224)
at ch.kit.sharp.main.MainApp.main(MainApp.java:15)
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
at com.sun.glass.ui.Application.checkEventThread(Application.java:445)
at com.sun.glass.ui.Screen.setEventHandler(Screen.java:245)
at com.sun.javafx.tk.quantum.QuantumToolkit.setScreenConfigurationListener(QuantumToolkit.java:600)
at javafx.stage.Screen.<clinit>(Screen.java:80)
... 4 more
like image 588
Reubaer Avatar asked Sep 02 '14 21:09

Reubaer


People also ask

What class do all JavaFX applications extend?

Class Application. Application class from which JavaFX applications extend. Waits for the application to finish, which happens when either of the following occur: the application calls Platform.

When a JavaFX program is launched the start method is called what type of object is passed to start?

The created stage object is passed as an argument to the start() method of the Application class (explained in the next section). A stage has two parameters determining its position namely Width and Height.

What is JavaFX application class must extend JavaFX application application?

This means you don't have a main method in your class as the error says. Replace the static with public static void main(String[] args) and it works. Also it is not possible to run a program without a main method. "it is not possible to run a program without main method" is not actually true.

Can a JavaFX be run without main method?

You can actually launch a JavaFX application without a main() method.


1 Answers

In addition to what Nejinx said, you must not directly call your start(), always call launch(), because it sets up the JavaFX environment, including creation of stage and calls start() passing the stage as an parameter to it.

The docs has a note specially stating this

NOTE: This method is called on the JavaFX Application Thread

The launch() can be called from any class, taking into consideration if the class is directly not extending javafx.application.Application, then you must pass the class extending it as an argument to the launch method.

For example, consider you have a class JavaFXMain which extends Application

class JavaFXMain extends Application {...}

You can use any other class, to start the JavaFX Application.

class Main {
   ...
   public void someMethod() {
      ...
      JavaFXMain.launch(JavaFXMain.class); // Launch the JavaFX application
      ...
   }
}

In your case, you can try something like this inside the main method of MainApp:

// You may remove args if you don't intend to pass any arguments
MainUIController.launch(MainUIController.class, args) 
like image 72
ItachiUchiha Avatar answered Oct 11 '22 15:10

ItachiUchiha