问题
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?
回答1:
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
回答2:
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.
回答3:
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);
回答4:
AFAIK, there is no API to have JavaFX stage always on top. But you can put JavaFX scene inside JFrame by using JFXPanel.
来源:https://stackoverflow.com/questions/12819194/javafx-2-2-stage-always-on-top