2

How does one push a JInternalFrame to the top of all the frames in a JDesktopPane?

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
davidahines
  • 3,976
  • 16
  • 53
  • 87

6 Answers6

3

try grabFocus() and requestFocus(). One of the should work. I personally used only requestFocus().

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    Doesn't work for me. Also (if it did work), according to the API, you should be using requestFocusInWindow(). – camickr Dec 02 '10 at 18:23
  • Didn't work. Ended up using desktop.getDesktopManager().activateFrame(jif); where JIF is a JInternalFrame – davidahines Dec 03 '10 at 19:06
  • neither grabFocus(), requestFocus() or transferFocus() works in this case. setSelected(true), worked for me. – lepe Dec 14 '11 at 03:39
2

Read the JInternalFrame API and follow the link to the Swing tutorial on "How to Use Internal Frames" where you will find a working example of how to "select" the active internal frame.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • DestopManager.activateFrame(frame) turned out to be the solution. I derived it from one of the examples in there. Thanks for the help. – davidahines Dec 03 '10 at 19:08
  • 1
    @dah, I would have thought frame.setSelected(true) would be easier. This is what the example uses every time a new frame is created. I've never seen the activateFrame() method referenced in the tutorials. – camickr Dec 03 '10 at 21:22
  • I have an app where setSelected also doesn't work on Ubuntu, but it works on Windows and Mac. – Chinasaur Jul 08 '11 at 19:18
  • @Chinasaur: I'm using Ubuntu and setSelected() seems to works fine. – lepe Dec 14 '11 at 03:37
2

In this example, a javax.swing.Action is used to select frames from a menu.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

The OP has noted that setSelected was not working, and he needed to call activateFrame manually. This sounds similar to an issue I was having with GTKLookAndFeel. I had an application that was all wired up to use setSelected to eventually trigger activateFrame. Worked fine with Windows and Mac native look and feel; activateFrame would get called automatically.

On Ubuntu, the system selected LaF was GTKLookAndFeel and for whatever reason this was not calling activateFrame. It didn't appear that setSelected was throwing an error or anything, it just wasn't getting around to calling activateFrame as the other LaFs seem to do. I think it's a GTKLookAndFeel compatibility issue.

In the end I punted on this and just prohibited GTKLookAndFeel, replacing it with Metal. Motif also had the compatible behavior (but it's so ugly...). The code looks something like this:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
if (UIManager.getLookAndFeel() instanceof com.sun.java.swing.plaf.gtk.GTKLookAndFeel)
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
Chinasaur
  • 2,108
  • 2
  • 16
  • 17
  • +1 Probably that's explains it. I'm using "Nimbus" in Ubuntu, and its working fine. – lepe Dec 14 '11 at 03:41
1

/*make current JInternalFrame deselected by calling JInternalFrame method setSelected(false)

*/then select new JInternalFrame using the same method; ie setSelected(true)

sample code:

try{ jframe1.setSelected(false); jframe2.setSelected(true); }catch (PropertyVetoException ex) {}

musthafa
  • 11
  • 2
1

Closing a modal JInternalFrame see the post by Mr. Zen(me)

Community
  • 1
  • 1
Mr. Zen
  • 704
  • 3
  • 7
  • 17