Ayaz Ali
Ayaz Ali

Reputation: 311

how to call jFrame on jDesktopPane without using JInternalFrame

I completed my project which assigned to me by university but now I am trying to create MDI for my project. I used 10 jFrame and one main form which is also jFrame, after that I add one Menu Bar, 10 jButtons for calling jFrame and one jDesktopPane for place calling jFrame. The below code using for calling jFrame place into jDesktopPane in all 10 jButton:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try
      {
        asd t = new asd();
        dskp.add(t);
        t.setVisible(true);

      }
      catch(Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
    } 

but not working with me and giving below error message:

java.lang.illegalargumentexception: adding a window to a container

How to do this and solve this issue because I didn't used any jInternal Frame. I think at this I am not able to use jInternale Frame because I did all work on jFrame such as full GUI with code and re-doing all work on jInternal Frame its not possible for me coz of short of time to submitting my final project.

Upvotes: 2

Views: 7260

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

If you're desiring to placing windows intp a JDesktopPane, then you need to use JInternalFrames. This is your best solution whether it is appealing to you or not.

A lesson in this is that you should strive to avoid creating classes that extend Swing components, especially top-level components such as JFrames, and instead create classes that produce JPanels, components that are flexible enough to be placed anywhere such as into JFrames, JInternalFrames, JDialogs, JOptionPanes, other JPanels, etc...

Note that a kludge is to get the contentPane from your JFrame, put it into a JInternalFrame and put that into the JDesktopPane, either that or set the JInternalPanes's contentPane with that from the JFrame. i.e.,

asd t = new asd();
JInternalFrame internalFrame = new JInternalFrame();
internalFrame.setContentPane(t.getContentPane());
internalFrame.pack();

// set the internalFrame's location
// ...

internalFrame.setVisible(true);
dskp.add(internalFrame);

But again note that this is a kludge and carries potential traps.

Upvotes: 5

Related Questions