Search code examples
javaeventsjavafxpopupwindow

JavaFX. Open a pop-up window before main


I have tried searching for this many times with no luck. I am writing a software where I want the user to input their resolution before moving on to the main UI, as the main UI will change size based on the resolution given. How would I open a popup window without a button event handler and then proceed to the main application?


Solution

  • You can just open the popup window in your start() method:

    public class MyApp extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            // make sure application doesn't exit when we close the dialog window:
            Platform.setImplicitExit(false);
    
            Stage popup = new Stage();
            TextField resolutionField = new TextField();
            // ... populate popup, etc...
    
            popup.setOnHidden(() -> {
    
                int resolution = Integer.parseInt(resolutionField.getText());
    
                // now create main UI...
    
                primaryStage.show();
            });
    
            popup.show();
        }
    }