Re: responding to JInternalFrame events from JDesktopPane/JFrame
Mike wrote:
I have a main window (JFrame) that has a variable JDesktopPane. To the
JDesktopPane, I added a couple of JInternalFrame children. The problem
I'm having is that I don't know at the JFrame and JDesktopPane level
when the JInternalFrame window closes. When the JInternalFrame window
closes, I need to do certain things in the parent JFrame. However, I
don't want to pass a handle from JFrame to JInternalFrame. How can I
listen for JInternalFrame window closing events at the main window
(JFrame) level? Is there a creative way to do this?
I don't often run into this because I often arrange code like this
foo()
showDialog()
bar()
Then bar() runs after the Dialog closes.
In circumstances where some object needs to know when something has
happened in another object (like the second one being closed) I
immediately think of listeners or observers. Lots of Java classes
already have provision for this. Checking the API usually helps me.
You say you don't want to pass a handle from JFrame to JInternalFrame.
Yet passing such a reference is a fairly natural thing to do and I see
nothing wrong with it.
I wouldn't pass to the JInternalFrame a reference to the JFrame. E.g.
... new MyInternalFrame(this);
...
class MyInternalFrame ... {
MyInternalFrame(JFrame frame) {
...
}
}
But I would think about why JInternalFrame needs this and adapt the
above accordingly. It's Ok to pass an instance of a Listener (e.g. an
InternalFrameListener). And if your frame implements the right variety
of Listener, this passed reference could be a reference to the frame ...
class MyFrame implements InternalFrameListener {
...
anInternalFrame = new MyInternalFrame();
anInternalFrame.addInternalFrameListener(this);
...
@Override
public void internalFrameClosed(InternalFrameEvent arg0) {
// do something?
}
@Override
public void internalFrameClosing(InternalFrameEvent arg0) {
// do something else?
}
Then the JInternalFrame can tell the JFrame that it is closing, without
introducing any undesirable coupling or interdependancies.
MyInternalFrame doesn't depend on a MyFrame.
--
RGB