I'm trying to create a window for an application. The size of the window is determined by getting the bounds of the primary screen. In order to do this I have created two classes.
RealMain class is the actual class that does everything:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class RealMain extends Application {
public static final double minWindowWidth;
public static final double minWindowHeight;
//Get primary screen bounds
static {
System.out.println(Thread.currentThread().getName());
Rectangle2D screenBounds = Screen.getPrimary().getBounds();
minWindowWidth = screenBounds.getWidth() / 2;
minWindowHeight = screenBounds.getHeight() / 1.5;
}
@Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(new HBox(), minWindowWidth, minWindowHeight);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
primaryStage.setMinWidth(primaryStage.getWidth());
primaryStage.setMinHeight(primaryStage.getHeight());
}
public static void main(String[] args) {
launch(args);
}
}
Main class is just an entry point into the program. All it does is call the main method of RealMain. Why do I need to use the Main class in the first place (why not just run the main method of RealMain)? It's because of a different problem I had, where I would get "Error: JavaFX runtime components are missing, and are required to run this application." when running the program as a jar on macOS. Apparently setting a regular java class as an entry point will fix this (one place where I found this solution is here), hence I needed to create the Main class.
public class Main {
public static void main(String[] args) {
RealMain.main(args);
}
}
It works fine when I run the main method of the RealMain class. However, if I use the main method of the Main class, I get the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at RealMain.<clinit>(RealMain.java:19)
at Main.main(Main.java:4)
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:441)
at com.sun.glass.ui.Screen.setEventHandler(Screen.java:369)
at com.sun.javafx.tk.quantum.QuantumToolkit.setScreenConfigurationListener(QuantumToolkit.java:684)
at javafx.stage.Screen.<clinit>(Screen.java:74)
... 2 more
So it seems when running the program from Main class, the main thread is used to execute the static block, which seems to be what causes the error (as to why, I have no idea). Meanwhile when running the program from RealMain class, the JavaFX Application Thread is used for the static block, which works fine. I guess the logical solution would be to somehow force the program to change threads and execute the static block on the JavaFX Application Thread, but I have no idea how to do that.
Could anyone please help me figure this out?