1

Suppose I have a class NewMDIApplication extends javax.swing.JFrame which contains a JDesktopPane and a JButton. Also suppose I have another class NewJInternalFrame extends javax.swing.JInternalFrame or NewJPanel extends javax.swing.JPanel which again contains some 'buttons' and 'text boxes' for performing some actions.

My intention is to load NewJInternalFrame's object or NewJPanel's object into JDesktopPane of NewMDIApplication when I click the JButton. Is it possible to be done? If yes, how to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ritesh
  • 314
  • 7
  • 19
  • 1
    You probably should not be extending `JFrame`, `JInternalFrame` **or** `JPanel`. In answer to your question, yes it is possible. As to how, what part are you having trouble with? Post an [SSCCE](http://sscce.org/) of your current code with a specific question. – Andrew Thompson Apr 16 '13 at 12:07
  • Actually Im designing `gui` using `netbeans' drag and drop functionality which automatically creates the codes. So, it would be troublesome if remove extends part. Both `NewMDIApplication` and `NewJPanel` are separate classes. I want to know which method should be used to load `NewJPanel`'s object into `JDesktopPane`. here is the code for both the classes: public class NewJFrame extends javax.swing.JFrame { – Ritesh Apr 16 '13 at 12:25
  • 1
    *"Actually Im designing gui using netbeans'"* It sounds like Netbeans is doing the 'doing' and you are its slave. I suggest you get around this by stop using Netbeans and learn how to code Java. As it is, I doubt anyone will be able to give you the 'magic incantation' to make Netbeans do this. – Andrew Thompson Apr 16 '13 at 13:09
  • surely I'll try to code my myself. I tried doing this `private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { new NewJInternalFrame().setVisible(true); jDesktopPane1.add(new NewJInternalFrame()); jDesktopPane1.validate(); } ` but nothing happens i.e when clicked on the buttons , internal frame doesn't appear. please suggest the correct way. Pardon me if I committed any mistake. – Ritesh Apr 16 '13 at 13:37

1 Answers1

1

Here is a working example of adding JInternalFrame instances to a JDesktopPane. Note that you need to set a size or they do not appear.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FrameWithInternalFrames {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

        @Override
        public void run() {
            // the GUI as seen by the user (without frame)
            JPanel gui = new JPanel(new BorderLayout());
            gui.setBorder(new EmptyBorder(2, 3, 2, 3));

            gui.setPreferredSize(new Dimension(400, 100));
            gui.setBackground(Color.WHITE);

            final JDesktopPane dtp = new JDesktopPane();
            gui.add(dtp, BorderLayout.CENTER);

            JButton newFrame = new JButton("Add Frame");

            ActionListener listener = new ActionListener() {
            private int disp = 10;
            @Override
            public void actionPerformed(ActionEvent e) {
                JInternalFrame jif = new JInternalFrame();
                dtp.add(jif);
                jif.setLocation(disp, disp);
                jif.setSize(100,100); // VERY important!
                disp += 10;
                jif.setVisible(true);
            }
            };
            newFrame.addActionListener(listener);

            gui.add(newFrame, BorderLayout.PAGE_START);

            JFrame f = new JFrame("Demo");
            f.add(gui);
            // Ensures JVM closes after frame(s) closed and
            // all non-daemon threads are finished
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            // See http://stackoverflow.com/a/7143398/418556 for demo.
            f.setLocationByPlatform(true);

            // ensures the frame is the minimum size it needs to be
            // in order display the components within it
            f.pack();
            // should be done last, to avoid flickering, moving,
            // resizing artifacts.
            f.setVisible(true);
        }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1 You can also `pack()` the internal frame, as shown [here](http://stackoverflow.com/a/7220544/230513), to leverage its layout. – trashgod Apr 16 '13 at 17:39