Sergey Maslov
Sergey Maslov

Reputation: 758

JavaFX does not get inside main() method

I'm very new to JavaFX. Before I started learning it I've been programming for android for year and half. Now I made a simple app with a single scene and a list view but it didn't go well. The problem is that when the scene is shown, code execution is stopped until I close this scene. I have hibernate's session factory initializing inside my main's class main() method, but the programm does not get inside it until I close the scene. I initialize the scene like this:

public class MyApp extends Application {
    public void start(Stage stage) {            
        Group root = new Group(circ);
        Scene scene = new Scene(root, 400, 300);

        stage.setTitle("My JavaFX Application");
        stage.setScene(scene);
        stage.show();
    }
}

How do I know the programm does not get inside main method? I put a breakpoint inside it and it only stoped at it when I closed the window(scene). Also, all hibernate initialization logs appear only I close it.

UPDATE: Main method

public static void main(String[] args) {
        launch(args);
        try {
            setUp();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        databaseEventNotifier = DatabaseEventNotifier.getInstance();
        databaseEventNotifier.notifyListeners();
    }

Upvotes: 0

Views: 316

Answers (1)

andrejb-dev
andrejb-dev

Reputation: 2468

Instead of the main method, you can use one of the initializer methods in java, like this instance initializer:

public class MyApp extends Application {
    {
        // here you can initialize hibernate and other stuff before the start method
    }

    public void start(Stage stage) {            
        // ... your start method
    }
}

or this static initializer:

public class MyApp extends Application {
    static {
        // here you can initialize hibernate and other stuff before the start method
    }

    public void start(Stage stage) {            
        // ... your start method
    }
}

the difference between them is explained in detail on the provided link, but in short I understand them in this way: static initializer is used to init static members and instance initializer for istance variables.

Upvotes: 1

Related Questions