2

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.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Ayaz Ali
  • 311
  • 2
  • 7
  • 16
  • 1
    Please strive to avoid unnecessary use of non-standard abbreviations in your posts on this site. There are several reasons for this, notably that programming is an exercise in precision. When you communicate here (or anywhere) about programming issues and questions, you want this communication to be as clear as possible to avoid any chance for ambiguity. I'd say at least half the communications on this site are requests for clarification. Let's avoid that. – Hovercraft Full Of Eels Dec 06 '12 at 19:22
  • first of all thanks for helping and next i will care it.... – Ayaz Ali Dec 07 '12 at 11:33

1 Answers1

5

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373