Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed JavaFX Stage inside JFXPanel (Swing Embed)

Tags:

java

swing

javafx

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?

like image 256
SnakeDoc Avatar asked Nov 12 '22 17:11

SnakeDoc


1 Answers

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

like image 51
Brian Blonski Avatar answered Dec 06 '22 11:12

Brian Blonski