*
The moose likes Swing / AWT / SWT and the fly likes How Do You Select a JFrame to Be the Front Window (that works for both Mac and Windows)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How Do You Select a JFrame to Be the Front Window (that works for both Mac and Windows)?" Watch "How Do You Select a JFrame to Be the Front Window (that works for both Mac and Windows)?" New topic
Author

How Do You Select a JFrame to Be the Front Window (that works for both Mac and Windows)?

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I have an Swing Application, using Java 5.0. For JFrame objects, I use the method called: requestFocus() on the JFrames and this causes the JFrame to become the front window, but this only works on Windows.

When I tested my app on a Mac, the request() focus method did nothing.

Does anybody have an idea of how to make a JFrame the front window in a platform-independent way?

Thanks for reading my question.
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
Hi! I've been trying to solve the same problem today (on Linux) and after some time of research I believe that there is no platform-independent, fully reliable way. There is another method, toFront(), but it does not work as expected. The problem is in what the JavaDocs say:

toFront()
Places this Window at the top of the stacking order and shows it in front of any other Windows in this VM. No action will take place if this Window is not visible. Some platforms do not allow Windows which own other Windows to appear on top of those owned Windows. Some platforms may not permit this VM to place its Windows above windows of native applications, or Windows of other VMs. This permission may depend on whether a Window in this VM is already focused. Every attempt will be made to move this Window as high as possible in the stacking order; however, developers should not assume that this method will move this Window above all other windows in every situation.


In my case, the frame (or rather the JDialog) was brought to front only when another frame of the same VM was focused earlier. I tried both requestFocus() and requestFocusInWindow() but with no success. As I read the behaviour is different for different versions of Windows, too (Vista behaves differently than XP and that differently than 98)... If you ever solve this, please let know in this thread.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Thanks for your help.

The method called: toFront() is working for me for both Vista and Mac OS 10.4. What I'm doing is implementing a Window menu like is customary on the Macintosh platform. The Window menu lists each window that you can select and when you select a menu-item, the corresponding window becomes the front window.

toFront() may not always work in every case, but it seems to be doing what I need it to do. I'll have to test is in XP and in Mac OS 10.5

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689



Well, maybe it's not working on the Mac yet. I'll focus on Windows Vista & XP for now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Do You Select a JFrame to Be the Front Window (that works for both Mac and Windows)?