Creating MDI Applications in Java Using Netbeans

This is a simple process for Java users who want to create Multiple Document Interface Applications in Java. The application makes use of JPanels , a JFrame and a JDesktopPane.

To start with, all your child forms should be created using panels (JPanels) as the container. A frame is then created as the parent form. A desktop pane is then added to the frame to be container for the panels on the JFrame.

Below is how the application Works

1.   Create your child forms on a panel

2.   Create a frame and add a desktoppane to it as shown below

main

The black color is showing the JDesktopPane. You can set the “Anchor” and “Auto Resizing” properties of the DesktopPane so that it fits the desired size of your choice.

Add the method below to your Frame Class

public void addFrame(JPanel panel, String title) {

try {

JInternalFrame[] children = jDesktopPane1.getAllFrames();

//creating a new internal frame from the Panel passes as parameter

internalframe j = new internalframe(panel, title);

//checking to see if the child form already exist

for (JInternalFrame f : children) {

if (f.getTitle().equals(j.getTitle())) {

f.setSelected(true);

return;

}

}

//adding a new child

jDesktopPane1.add(j);

j.setVisible(true);

j.setSelected(true);

} catch (PropertyVetoException ex) {

Logger.getLogger(mainxpframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

To show the child forms add the code below to the button click event

customersdetailpanel sd=new customersdetailpanel();

sd.setSize(750, 600);

addFrame(sd, "Customers Information");

the “customersdetailpanel” is one of the child forms.

Below is an interface showing showing the MDI Application

sub

Hope this will be helpful

Advertisements

14 thoughts on “Creating MDI Applications in Java Using Netbeans

  1. Hi ,
    Really this was the simplest and easiest tutorial around the web ! i am working on a project and needed to build a gui using Java ! I had worked with Web interface but this seems kinda different , The information is really nice !

  2. This tutorial was really helpful. I’m developing a MDI app for web designers and searching how it realizes. Thanks a lot from Japan.

    • thanks for your comment
      i added a jinternal frame and added a constructor to accept a Jpanel and a string as its title

      below is the class

      public class internalframe extends javax.swing.JInternalFrame {

      private Container contentPane = null;
      /** Creates new form InternalFrame */
      public internalframe() {
      initComponents();
      }

      public internalframe( JPanel panel, String title) {
      //super(parent, modal);
      contentPane = this.getContentPane();
      initComponents();
      this.setSize(panel.getSize().width + 20, panel.getSize().height + 30);
      panel.setLocation(0, 0);
      contentPane.add(panel);
      this.setTitle(title);
      repaint();

      }
      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings(“unchecked”)
      //
      private void initComponents() {

      setClosable(true);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 394, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 282, Short.MAX_VALUE)
      );

      pack();
      }//

      // Variables declaration – do not modify
      // End of variables declaration

      }

  3. i hav 1 problem in showing the child forms on the button click…

    i hav used jinternalframes as child forms… i want to open another jinternalframe ( i m currently trying for..) on click of one of buttons on my form..

    my mdi menubar works properly but i hav nw this problem…
    plz help me.. rply me as quick as possible… thnx in advance.

  4. I’m facing a huge problem concerning a close button in a jpanel which close only the this panel not all the application(the app has many jpanels). plz if anyone has the appropriate code don’t hesitate to post it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s