9

How to get active (having focus) frame (JInternalFrame) that is inside JDesktopPane? I need it for my MDI notepad (not that anybody would use that, just a training project). Looking at api, I see only functions to get all JInternalFrames, not active one.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dariusz G. Jagielski
  • 655
  • 3
  • 11
  • 22

3 Answers3

12

Use JDekstopPane.getSelectedFrame() method (From doc: currently active JInternalFrame in this JDesktopPane, or null if no JInternalFrame is currently active.) or JDesktopPane.getAllFrames() to get list of all JInternalFrames currently displayed in the desktop and check isSelected() method.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

Make a List<JInternalFrame> and check isSelected() as you iterate though it.

Addendum: See also these examples that use Action to select an internal frame from a menu.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 3
    Note that [JDesktopPane.getAllFrames()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JDesktopPane.html#getAllFrames()) returns an array, so you might as well just iterate the array (instead of using a list). – Andrew Thompson Dec 17 '11 at 04:42
  • 1
    -1 for reinventing the wheel. Either getSelectedFrame() or getAllFrames() would be used. – camickr Dec 17 '11 at 05:23
2

Have you looked at the Java tutorial titled How to Use Internal Frames? In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. Activated means the internal frame was brought to the top; deactivated means it's no longer on top. Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it.

Don't iterate over all the panes - use events.

If for some reason you prefer to poll your UI rather than use an event-driven approach you can call getSelectedFrame which returns the active JInternalFrame. I'm not sure why no one else mentioned it.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Paul
  • 19,704
  • 14
  • 78
  • 96
  • @trashgod it's pretty straightforward, as `JInternalFrames` are created an `InternalFrameListener` is attached to each that keeps track of which frame is active and which is not. I've updated my answer a bit in case the poster doesn't care for events - `JDesktopPane` has the method `getSelectedFrame`. – Paul Dec 17 '11 at 05:00
  • > In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. > Activated means the internal frame was brought to the top; deactivated means it's no longer on top. > Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it." In other words another tip on "How to make your code more confusing and less straightforward". Thank you, though I'll keep my code so simple that even if you never used Java you'd understand it. – Dariusz G. Jagielski Dec 17 '11 at 05:15
  • -1 for reinventing the wheel. getSelectedFrame() was mentioned earlier. – camickr Dec 17 '11 at 05:26
  • @camickr Polling the UI and listening for UI events are two completely different ways of handling the problem. Since you're not familiar with the difference here's an example. Let's say you have `java.swing.Action`s that back menu items and toolbar buttons. The `Action` (and thus the items and buttons) should only be enabled when certain frames are on top. Would you rather run a timer asking the `JDesktopPane` who's on top or just listen for events? Reinventing the wheel would be iterating over all the frames looking for `isSelected` when `getSelectedFrame` has been around since Java 1.3. – Paul Dec 17 '11 at 05:28
  • @DariuszG.Jagielski, see my response to camickr, and good luck responding to changes in the UI. – Paul Dec 17 '11 at 05:29
  • The question asked "how to get the active frame", not "how to track frame activation/deactivation". Your suggestion to use a listener and handle events to track the active frame is reinventing the wheel since a method exists to do this already. Agreed it is a solution for a different problem, just not this problem. – camickr Dec 17 '11 at 05:33
  • @camickr: I rolled back my careless edit; Paul had already linked to `InternalFrameListener`, which seems like a good use of the API. Edit: Sorry if I adversely influenced your appraisal. – trashgod Dec 17 '11 at 05:34