Search code examples
javaswingfocusjinternalframejdesktoppane

Is there a method to know when the focus of an internal frame has changed to a different one within a desktop pane?


I have a JDesktopPane which creates several JInternalFrame components. The desktop keeps some variables which each internal frame needs to know, so every time an internal frame is clicked, it needs to check the desktop variables and copy them. When an internal frame is created, I can just copy them, but once the frame has been created I don't know how to make it check the variables every time it is clicked.

The thing is that if I use the listener on the internal frame, I can't use desktop's methods and from the desktop I don't know how to check if an internal frame has been clicked (because it creates the internal frames on run-time). What I thought was to use a method in the JDesktopPanethat copies the variables and is called every time any of the internal frames is clicked (for example, if any part of the desktop is clicked at all), but I don't know what event is triggered in then desktop when the internal frames are clikced (if there's any at all).

So, is there any method on the desktop pane (or wherever) which is activated every time an internal frame is selected?


Solution

  • but I don't know what event is triggered in then desktop when the internal frames are clikced

    You can use an InternalFrameListener.

    Read the section from the Swing tutorial on How to Write and InternalFrameListener for more information and working examples.

    I can't use desktop's methods

    Why not?

    You can get the desktop pane using the getDesktopPane() method of the internal frame.