Design java swing application with multiple windows

This article will describe how to design a java swing application with multiple windows.The multiple windows will be created with the help of multiple overlapping jPanels of same size within the main jFrame.The example source code will fully illustrate this.You will thus be able to invoke multiple gui windows on click event, where actually one single jPanel out of the multiple jPanels will be set as visible and the remainder hidden.

I use the netbeans IDE but these guidelines will work for others too.

To begin add a jFrame to your application and set it as the Main class of your application from the project configuration settings.

Size your jFrame as per your requirements.

Add a menu from the palette as you would need to use menus for navigation between the windows.Insert menu items to identify each window you would like to open.

Now come the windows – Insert as many jPanels to the main JFrame ,depending on the number of multiple windows you desire.Give each jPanel a unique variable name such as jPanelWindow1 … etc

Click on Design View in the Main Panel and go to the Navigator where components are listed.Select each jPanel one by one and size it to fill the JFrame completely.The custom design of each jPanel can be done later.For now you can insert a Label in the jPanel saying it is Window 1 ,Window 2 etc.

To select a jPanel for inserting a component you have to double click it in the component navigator(This is important). You can also use the property sheets on right clicking the Jpanel to set the height and width of the Jpanel

By now you have a jFrame as your main class with multiple jPanels, each sized to fit the jFrame,each Jpanel with a unique variable name.Each Jpanel has a label to identify itself when displayed (Label : Window 1 etc).You also have a menu structure to navigate to the Jpanels

Now you can begin code

in the actionperformed event of the menu item for invoking the first window add sample code as under.The following is just sample code to be used as a guideline and will be different for your application

private void jMenuItemWindow1ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(true);
          jPanelWindow2.setVisible(false);
          jPanelWindow3.setVisible(false);
     }                                                

     private void jMenuItemWindow2ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(false);
          jPanelWindow2.setVisible(true);
          jPanelWindow3.setVisible(false);
     }                                                

     private void jMenuItemWindow3ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(false);
          jPanelWindow2.setVisible(false);
          jPanelWindow3.setVisible(true);
     }             

And thats it , It works

I also am giving below the complete source code of the swing application class

package multiplewindows;

/**
 *
 * @author 
 */
public class mainGUI extends javax.swing.JFrame {

     /**
      * Creates new form mainGUI
      */
     public mainGUI() {
          initComponents();
     }

     /**
      * 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")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
     private void initComponents() {

          jPanelWindow1 = new javax.swing.JPanel();
          jLabel3 = new javax.swing.JLabel();
          jPanelWindow2 = new javax.swing.JPanel();
          jLabel2 = new javax.swing.JLabel();
          jPanelWindow3 = new javax.swing.JPanel();
          jLabel1 = new javax.swing.JLabel();
          jMenuBar1 = new javax.swing.JMenuBar();
          jMenu1 = new javax.swing.JMenu();
          jMenuItemExit = new javax.swing.JMenuItem();
          jMenu2 = new javax.swing.JMenu();
          jMenuItemWindow1 = new javax.swing.JMenuItem();
          jMenuItemWindow2 = new javax.swing.JMenuItem();
          jMenuItemWindow3 = new javax.swing.JMenuItem();

          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

          jPanelWindow1.setName("jPanelWindow1"); // NOI18N

          jLabel3.setText("This is Window 1");
          jLabel3.setName("jLabel3"); // NOI18N

          javax.swing.GroupLayout jPanelWindow1Layout = new javax.swing.GroupLayout(jPanelWindow1);
          jPanelWindow1.setLayout(jPanelWindow1Layout);
          jPanelWindow1Layout.setHorizontalGroup(
               jPanelWindow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow1Layout.createSequentialGroup()
                    .addGap(122, 122, 122)
                    .addComponent(jLabel3)
                    .addContainerGap(199, Short.MAX_VALUE))
          );
          jPanelWindow1Layout.setVerticalGroup(
               jPanelWindow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow1Layout.createSequentialGroup()
                    .addGap(101, 101, 101)
                    .addComponent(jLabel3)
                    .addContainerGap(164, Short.MAX_VALUE))
          );

          jPanelWindow2.setName("jPanelWindow2"); // NOI18N

          jLabel2.setText("This is Window 2");
          jLabel2.setName("jLabel2"); // NOI18N

          javax.swing.GroupLayout jPanelWindow2Layout = new javax.swing.GroupLayout(jPanelWindow2);
          jPanelWindow2.setLayout(jPanelWindow2Layout);
          jPanelWindow2Layout.setHorizontalGroup(
               jPanelWindow2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow2Layout.createSequentialGroup()
                    .addGap(122, 122, 122)
                    .addComponent(jLabel2)
                    .addContainerGap(199, Short.MAX_VALUE))
          );
          jPanelWindow2Layout.setVerticalGroup(
               jPanelWindow2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow2Layout.createSequentialGroup()
                    .addGap(101, 101, 101)
                    .addComponent(jLabel2)
                    .addContainerGap(164, Short.MAX_VALUE))
          );

          jPanelWindow3.setName("jPanelWindow3"); // NOI18N

          jLabel1.setText("This is Window 3");
          jLabel1.setName("jLabel1"); // NOI18N

          javax.swing.GroupLayout jPanelWindow3Layout = new javax.swing.GroupLayout(jPanelWindow3);
          jPanelWindow3.setLayout(jPanelWindow3Layout);
          jPanelWindow3Layout.setHorizontalGroup(
               jPanelWindow3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow3Layout.createSequentialGroup()
                    .addGap(122, 122, 122)
                    .addComponent(jLabel1)
                    .addContainerGap(199, Short.MAX_VALUE))
          );
          jPanelWindow3Layout.setVerticalGroup(
               jPanelWindow3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(jPanelWindow3Layout.createSequentialGroup()
                    .addGap(101, 101, 101)
                    .addComponent(jLabel1)
                    .addContainerGap(164, Short.MAX_VALUE))
          );

          jMenuBar1.setName("jMenuBar1"); // NOI18N

          jMenu1.setText("File");
          jMenu1.setName("jMenu1"); // NOI18N

          jMenuItemExit.setText("Exit");
          jMenuItemExit.setName("jMenuItemExit"); // NOI18N
          jMenu1.add(jMenuItemExit);

          jMenuBar1.add(jMenu1);

          jMenu2.setText("Open");
          jMenu2.setName("jMenu2"); // NOI18N

          jMenuItemWindow1.setText("Window 1");
          jMenuItemWindow1.setName("jMenuItemWindow1"); // NOI18N
          jMenuItemWindow1.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItemWindow1ActionPerformed(evt);
               }
          });
          jMenu2.add(jMenuItemWindow1);

          jMenuItemWindow2.setText("Window 2");
          jMenuItemWindow2.setName("jMenuItemWindow2"); // NOI18N
          jMenuItemWindow2.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItemWindow2ActionPerformed(evt);
               }
          });
          jMenu2.add(jMenuItemWindow2);

          jMenuItemWindow3.setText("Window 3");
          jMenuItemWindow3.setName("jMenuItemWindow3"); // NOI18N
          jMenuItemWindow3.addActionListener(new java.awt.event.ActionListener() {
               public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItemWindow3ActionPerformed(evt);
               }
          });
          jMenu2.add(jMenuItemWindow3);

          jMenuBar1.add(jMenu2);

          setJMenuBar(jMenuBar1);

          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
               layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jPanelWindow1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelWindow2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelWindow3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
               layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jPanelWindow1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelWindow2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelWindow3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );

          pack();
     }// </editor-fold>                        

     private void jMenuItemWindow1ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(true);
          jPanelWindow2.setVisible(false);
          jPanelWindow3.setVisible(false);
     }                                                

     private void jMenuItemWindow2ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(false);
          jPanelWindow2.setVisible(true);
          jPanelWindow3.setVisible(false);
     }                                                

     private void jMenuItemWindow3ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
          // TODO add your handling code here:
          jPanelWindow1.setVisible(false);
          jPanelWindow2.setVisible(false);
          jPanelWindow3.setVisible(true);
     }                                                

     /**
      * @param args the command line arguments
      */
     public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
               for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                         javax.swing.UIManager.setLookAndFeel(info.getClassName());
                         break;
                    }
               }
          } catch (ClassNotFoundException ex) {
               java.util.logging.Logger.getLogger(mainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
               java.util.logging.Logger.getLogger(mainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
               java.util.logging.Logger.getLogger(mainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
               java.util.logging.Logger.getLogger(mainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //</editor-fold>

          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                    new mainGUI().setVisible(true);
               }
          });
     }
     // Variables declaration - do not modify                     
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel2;
     private javax.swing.JLabel jLabel3;
     private javax.swing.JMenu jMenu1;
     private javax.swing.JMenu jMenu2;
     private javax.swing.JMenuBar jMenuBar1;
     private javax.swing.JMenuItem jMenuItemExit;
     private javax.swing.JMenuItem jMenuItemWindow1;
     private javax.swing.JMenuItem jMenuItemWindow2;
     private javax.swing.JMenuItem jMenuItemWindow3;
     private javax.swing.JPanel jPanelWindow1;
     private javax.swing.JPanel jPanelWindow2;
     private javax.swing.JPanel jPanelWindow3;
     // End of variables declaration                   
}


That is all.

If you liked this article please do leave a reply and share it with friends.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.