简体   繁体   中英

JavaFX UNDECORATED Stage not showing

I've got problem with may e(fx)clipse application. I want to show a splash screen upon application startup. I successfully created class implementing StartupProgressTrackerService , and got my stateReached method invoked. However I've got problems with javafx itself. I want to create Stage with StageStyle.UNDECORATED . However when i invoke stage.show() method stage isn't rendered immediately and appears just after main window is created. It works fine eg with StageStyle.UTILITY . It also renders correctly when i use showAndWait() method, but it stops my app from loading until i close the stage.

Here is my code:

public class MyStartupProgressTrackerService implements StartupProgressTrackerService {

    private Stage stage;

    public MyStartupProgressTrackerService() {

    }

    @Override
    public OSGiRV osgiApplicationLaunched(IApplicationContext applicationContext) {
        applicationContext.applicationRunning();
        return StartupProgressTrackerService.OSGiRV.CONTINUE;
    }

    @Override
    public void stateReached(ProgressState state) {
        if (DefaultProgressState.JAVAFX_INITIALIZED.equals(state)) {
            stage = new Stage(StageStyle.UNDECORATED);
            stage.initModality(Modality.WINDOW_MODAL);
            stage.setAlwaysOnTop(true);
            ImageView view = null;
            try {
                view = new ImageView(SPLASH_IMAGE);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            BorderPane bp = new BorderPane();
            bp.getChildren().add(view);
            Scene scene = new Scene(bp, 400, 300);
            stage.setScene(scene);
            stage.show();
        }
    }

}

I found an ugly solution, but, at least, it works. I noticed that method stage.showAndWait() as a side effect finishes building all controls which haven't been rendered yet. So the trick is to initialize splash screen, and then create dummy stage, showAndWait() it and close() immediately. I know that this solution is far from ideal, so i would appreciate it if someone could show me alternate way to make it work :)

My code:

public void showSplash() {
    splashScreen = createSplashScreen();
    Stage stage2 = new Stage(StageStyle.TRANSPARENT);
    splashScreen.show();
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            stage2.close();
        }
    });
    stage2.showAndWait();
}

private Stage createSplashScreen() {
    Stage stage = new Stage(StageStyle.UNDECORATED);
    stage.setAlwaysOnTop(true);

    VBox vbox = new VBox();
    vbox.getChildren().add(new ImageView(splashImage));
    Scene scene = new Scene(vbox, 400, 300);
    stage.setScene(scene);
    return stage;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM