简体   繁体   中英

Embed JavaFX Stage inside JFXPanel (Swing Embed)

I'm trying to modify an existing JavaFX GUI program to have a feature of "Always Stay On Top". This is not supported in JavaFX at the moment (why!?!?), so some googling has led me to believe I must marry Swing into my JavaFX application in order to achieve this. Not ideal, but OK.

All example patterns I've found use the JFXPanel instead of Stage , however since my application already exists and was written using JavaFX from the start (zero Swing is in the codebase at the moment), removing the Stage from the application starts to become a major re-write just to support this "on top" feature.

How can I embed an existing JavaFX Stage in a JFXPanel or similar, that will allow me to use the standard getSomeSwingWindowOrPanel().setAlwaysOnTop(true); and make my application float on top of all other windows as expected? Am I going about this wrong, ie. is there a better way such as using JNI or something?

A setAlwaysOnTop() method was added to stages in Java 8u20.

public final void setAlwaysOnTop(boolean value)

Sets the value of the property alwaysOnTop.

Property description:

Defines whether this Stage is kept on top of other windows. If some other window is already always-on-top then the relative order between these windows is unspecified (depends on platform).

There are differences in behavior between applications if a security manager is present. Applications with permissions are allowed to set "always on top" flag on a Stage. In applications without the proper permissions, an attempt to set the flag will be ignored and the property value will be restored to "false".

The property is read only because it can be changed externally by the underlying platform and therefore must not be bindable.

Default value:

false

Since:

JavaFX 8u20

All example patterns I've found use the JFXPanel instead of Stage, however since my application already exists and was written using JavaFX from the start (zero Swing is in the codebase at the moment), removing the Stage from the application starts to become a major re-write just to support this "on top" feature.

A better way to add "Always Stay On Top" feature (without removing the Stage ) is:-

stage.initModality(Modality.APPLICATION_MODAL); .

I hope it works for you........

Cheers

[1] http://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html

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