Link to home
Start Free TrialLog in
Avatar of svekke
svekke

asked on

How to refer between JInternalFrames.

Hello,

I'm trying to open a JinternalFrame when pressing a button in another JinternalFrame. In order to accomplish this I need to add a JDesktopPane to the JinternalFrame which opens the other JInternalFrame.
To start I set the visibility of this JDesktopPane to false. And when I press the Edit button to open the JInternalFrame I set the visibilty to true and add the other JInternalFrame to this JDesktopPane.
But how do I set the visibility of this JDesktopPane to false when I close or dispose the JInternalFrame I opened in order to edit the data?
How do I refer to the parent JinternalFrame from the child JInternalFrame?


Thanx in advance.
Avatar of zzynx
zzynx
Flag of Belgium image

>> In order to accomplish this I need to add a JDesktopPane to the JinternalFrame which opens the other JInternalFrame.
Shouldn't you add a JDesktopPane to your main JFrame and open all JInternalFrames form there?

>> how do I set the visibility of this JDesktopPane to false when I close or dispose the JInternalFrame
By adding an InternalFrameListener
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The pattern you need is to use a controller class. This can be the class that owns the desktop pane. Pass requests to *it* for the other frame
>> How do I refer to the parent JinternalFrame from the child JInternalFrame?
You could do it by simply passing it.
But I believe you better pass the JDesktopPane or even better the main JFrame.

public class MyInternalFrame extends JInternalFrame {

         private JDesktopPane desktopPane = null;    // <<< Make your choice between this
         private JFrame mainFrame = null;                   // <<< or this

         public MyInternalFrame(JDesktopPane dp) {
              desktopPane = dp;
         }

         OR

        public MyInternalFrame(JFrame f) {
             mainFrame = f;
        }


        Then further on (in all methods you want) you can use mainFrame or desktopPane as you need
}
JInternalFrame otherFrame = controller.getInternalFrame("theOneIWant");

The controller can keep a Map<String, JInternalFrame>
Of course if you don't like my recommendation, you can pass the parent JInternalFrame too:

public class MyInternalFrame extends JInternalFrame {

         private JInternalFrame parentInternalFrame = null;

         public MyInternalFrame(JInternalFrame parent) {
              parentInternalFrame = parent;
         }


        Then further on (in all methods you want) you can use parentInternalFrame as you need
}

Remark:
Of course you need one of the other JInternalFrame constructors, just add an extra parameter:

  public MyInternalFrame(JInternalFrame parent, String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
      super(title, resizable, closable, maximizable, iconifiable);
      parentInternalFrame = parent;
  }
Forgot the word if:
>> Of course IF you need one of the other JInternalFrame constructors
As you create your JInternalFrame:

JFrame editFrame = new JInternalFrame();
...
frameMap.put("editFrame", editFrame);

Of course, the controller can use its own Map too

The following convenience method can be used in the controller:


public JInternalFrame getInternalFrame(String name) {
    return (JInternalFrame)frameMap.get(name);
}

Thanx 4 axxepting