This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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?
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:
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.
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