JavaFX 2.2 Stage always on top
Asked Answered
R

4

17

I have a normal JFrame (one part of the app) and a second JavaFX window (I can't use a JFrame for the JavaFX stage). The problem is that the JavaFX window should always be on top of all other windows.

I have no idea how I should solve this problem! Any ideas?

Ratter answered 10/10, 2012 at 12:19 Comment(2)
There is a feature request in JavaFX-Jira for this, but unfortunately the latest comment on this issue was made in 2011.I wish things were moving faster for such a trivial-looking feature.Drees
This guys nailed it on the head. #15828099Dissidence
B
36

I know this is a old thread, but things keep on changing. Coming to JDK 8u20 is a new method primaryStage.setAlwaysOnTop(true);

This would be the easiest way to make a stage always on top. For early access to 8u20 visit the website.

public class KeyholeDemo extends Application {
    @Override public void start(Stage primaryStage) {
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setAlwaysOnTop(true);

        // code omitted...
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Sample code taken from this nice writeup

Burkitt answered 31/5, 2014 at 3:43 Comment(1)
This is the way and worked for me. stage.initModality(Modality.APPLICATION_MODAL) also work at some point but the stage will be still behind the task bar.Addend
P
14

I have a similar problem right now.

I use this line of code to get an always on top effect:

stage.initModality(Modality.APPLICATION_MODAL);

It works fine for me.

Here is the doc.

Perimeter answered 1/4, 2013 at 18:44 Comment(2)
Thanks mate, this worked also for me. Strange method, but seems to be the only easy way. GreetsClementia
Thanks a lot ! By giving this code you saved lot of efforts !!Hauler
C
2

Like this (I'm using Alert)

 Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
 stage.setAlwaysOnTop(true);

If you want it on top always blocking other windows, just set:

alert.initModality(Modality.APPLICATION_MODAL);
Chaffin answered 13/7, 2017 at 12:0 Comment(0)
S
1

AFAIK, there is no API to have JavaFX stage always on top. But you can put JavaFX scene inside JFrame by using JFXPanel.

Sapphire answered 10/10, 2012 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.