JavaFX - Always On Top


Stage is the top level container in JavaFX. It provides attributes such as fullScreen, iconified, opacity, style, icons, resizable, title which loosely maps to corresponding methods in java.awt.Frame.

But it doesn't provide any functionality to create Dialog behavior (modal, no task bar icon etc) and also support for setAlwaysOnTop. These two features are required to get the actual Widget feel. As of now the Stage shows a taskbar icon for each application.

So here is a workaround.. Create an instance of AWTEventListener. Add the same to default AWT Toolkit so as to listen to all ComponentEvent. This will receive an event when Stage (Frame) resized and shown. Get the instance of Frame from this event. Retrieve the content of the Frame and add it to new instance of Dialog. Dispose the Frame. Now set various attributes of Dialog such as modality, always-on-top and make this Dialog visible. Now the content of Frame will be shown in Dialog! :) We may also apply translucency, shaped windows etc to newly created dialog using AWT APIs.

A simple implementation of above approach is provided in FXDialogUtils. This class must be instantiated before Stage is shown so that it can listen to all events. Example, the code can be embedded in Calendar sample as shown below. That is just adding one line to existing application.


FXDialogUtils { };

Stage {
    
    x: bind stageX with inverse
    y: bind stageY with inverse
    width: fxCalendar.width
    height: fxCalendar.height
    title: "Calendar"

If you just want to change some attributes of Frame, you may directly obtain the Frame instance using Frame.getFrames(). This will return an array of Frame instance. We can get find the frame instance of specified Stage using its title. We can modify various Window or Frame properties using this instance.

We may also directly obtain the instance of AWT Window equivalent of Stage using some JavaFX internal APIs, but that may not be compatible across releases. But yes, that is another way of doing it

Now you can launch the Calendar as real widget

Yes, its a hack! Keep exploring other possibilities!

<script type="text/javascript">var dzone_url = "http://blogs.sun.com/rakeshmenonp/entry/javafx_always_on_top";</script> <script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>

Comments:

Good hack.. This is really good.

Posted by Raghu Nair on April 30, 2009 at 07:50 AM IST #

The JFXtras project has a drop-in replacement for Stage called JFXStage that has some niceties, such as alwaysOnTop, and direct access to the underlying java.awt.Window object:
http://code.google.com/p/jfxtras/

It actually uses a similar hack to get at the underlying window, with the advantage that it is maintained for each JavaFX release (I just finished updating the source to handle differences in the upcoming Marina release).

Posted by Stephen Chin on May 03, 2009 at 10:28 PM IST #

@Stephen Chin Yes, JFXtras is good alternative. But I think it relies on non-public APIs - (com.sun.javafx.\* in org.jfxtras.stage.WindowHelper). I just wanted to suggest other options.

Posted by Rakesh Menon on May 04, 2009 at 02:31 AM IST #

Post a Comment:
Comments are closed for this entry.
About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today