Hey Lifesavers! How can i dispose the current frame holding the combobox on item selection to navigate to the next frame.

The dispose(); method does not dispose the Main class, when i select supplier item onthe combo list.

how can i reolve this;

sample code from the sodes is below.


main frame code
==============================

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

    public class Main extends JFrame implements ItemListener
   {
      private JLabel logo;
      private JLabel line1;
      private JLabel line2;
      private JLabel line3;
      private JButton exitbtn;
      private JComboBox Selector;
      private JLabel task;
      private JButton logoutbtn;
      private String [] SelectorItems;
   
   
       public Main()
      {
        //construct preComponents
         SelectorItems = new String [9];
      
         SelectorItems [0] =  "      -----  Select a  Form  -----";
         SelectorItems [1] = "Supplier";
         SelectorItems [2] = "Order"; 
         SelectorItems [3] = "Warehousing"; 
         SelectorItems [4] = "Product"; 
         SelectorItems [5] = "Employee";
         SelectorItems [6] = "Inventory";
         SelectorItems [7] = "Sale";
         SelectorItems [8] = "Exit";
      	
         Selector = new JComboBox (SelectorItems);
      
        //construct components
         logo = new JLabel (" WELCOME TO CHOPPIES MANAGEMENT SYSTEM");
         line1 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         line2 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         line3 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         exitbtn = new JButton ("Exit");
         Selector = new JComboBox (SelectorItems);
         task = new JLabel ("Task Selection :");
         logoutbtn = new JButton ("Logout");
      	
      	 // set pictures     
         ImageIcon labelIcon2 = new ImageIcon("logo2.png");
         JLabel cornerLabel = new JLabel( labelIcon2 );
         add(cornerLabel);
         cornerLabel.setBounds( 60,  130, 500, 92);
      	
      
      	
         Selector.setForeground (Color.red);
        //Selector.setFont (SansSerif);
      
      /* Initialization */
         Selector.setSelectedIndex (0);
         Selector.setBackground (Color.cyan);
      
        //adjust size and set layout
         getContentPane().setBackground(Color.gray);
         setTitle("Main");
         setSize(624, 537);
         setLayout (null);
      
      
        //add components
         add (logo);
         add (line1);
         add (line2);
         add (line3);
         add (exitbtn);
         add (Selector);
         add (task);
         add (logoutbtn);
      
        //set component bounds (only needed by Absolute Positioning)
         logo.setBounds (200, 85, 305, 25);
         line1.setBounds (-35, 445, 780, 15);
         line2.setBounds (-100, 55, 765, 25);
         line3.setBounds (-75, 110, 765, 25);
         exitbtn.setBounds (335, 415, 75, 20);
         Selector.setBounds (215, 240, 190, 25);
         task.setBounds (80, 240, 155, 30);
         logoutbtn.setBounds (215, 415, 75, 20);
      	
         Selector.addItemListener (this);
      	
         ButtonHandler handler = new ButtonHandler(); 
      			          
         exitbtn.addActionListener( handler );           
         logoutbtn.addActionListener( handler);
      
         
      	
      }
       private class ButtonHandler implements ActionListener
      {
      
        // handle button event
          public void actionPerformed( ActionEvent e )
         {
                     
            Object source = e.getSource();
         	
            if(source == exitbtn)
            {
               int returnValue = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit the system", 
                  					"Exit Confirmation", JOptionPane.YES_NO_OPTION);
            							
               if(returnValue == JOptionPane.YES_OPTION)
               {
               
               
                  System.exit(1);
               }
                              
            }
            else if(source == logoutbtn)
            {
               login frame = new login();
               frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
               frame.setLocationRelativeTo(null);
               frame.toFront();
               frame.setVisible (true);
               frame.setResizable(false);
               dispose();
                              
            }
         
         }
      }
   
       public void itemStateChanged(ItemEvent e) 
       		
      {
         int Selection;
      	
         Selection = Selector.getSelectedIndex();
      	
         if (Selection == 0)
         {
            
         } 
         else if (Selection == 1)
         {
            
            supplier frame = new supplier();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.toFront();
            frame.setVisible (true);
            frame.setResizable(false);
            dispose();
         } 
         else if (Selection == 2) 
         {
            order frame = new order();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.toFront();
            frame.setVisible (true);
            frame.setResizable(false);
            dispose();
         } 
         else if (Selection == 3)
         {
            
         } 
         else if (Selection == 4) 
         {
         
            addproduct frame = new addproduct();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.toFront();
            frame.setVisible (true);
            frame.setResizable(false);
            dispose();
           
         } 
         else if (Selection == 5) 
         {
            addEmployee frame = new addEmployee();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.toFront();
            frame.setVisible (true);
            frame.setResizable(false);
            dispose();
         } 
         else if (Selection == 6) 
         {
            
         } 
         else if (Selection == 7)
         {
            sale frame = new sale();
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.toFront();
            frame.setVisible(true);
            frame.setResizable(false);
            dispose(); 
         
         } 
         else if (Selection == 8) 
         {
            int returnValue = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit the system", 
                  					"Exit Confirmation", JOptionPane.YES_NO_OPTION);
            							
            if(returnValue == JOptionPane.YES_OPTION)
            {
               
               
               System.exit(1);
            }
         } 
      } 
      
   
   
       public static void main (String[] args)
      {
         Main frame = new Main ();
         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         frame.setLocationRelativeTo(null);
         frame.toFront();
         frame.setVisible (true);
         frame.setResizable(false);
      }
   }

=========================================

supplier frame code

=========================================

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

    public class supplier extends JFrame
   {
   
      private JLabel logo;
      private JLabel line1;
      private JLabel line2;
      private JLabel line3;
      private JButton exitbtn;
      private JButton logoutbtn;
      private JTextField nametxt;
      private JTextField locationtxt;
      private JTextField addresstxt;
      private JTextField telnotxt;
      private JTextField paydaytxt;
      private JLabel suppliernamelbl;
      private JLabel namelbl;
      private JLabel locationlbl;
      private JLabel addresslbl;
      private JLabel telnolbl;
      private JLabel paydaylbl;
      private JButton savebtn;
      private JButton mainbtn;
      private JLabel currentorderlbl;
      private JTextField currentordertxt;
      private JTextField deliverydatetxt;
      private JTextField supplieridtxt;
      private JLabel deliverydatelbl;
      private JLabel supplieridlbl;
   
       public supplier()
      {  
      
         logo = new JLabel ("CHOPPIES MANAGEMENT SYSTEM");
         line1 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         line2 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         line3 = new JLabel ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
         exitbtn = new JButton ("Exit");
         logoutbtn = new JButton ("Logout");
         nametxt = new JTextField (5);
         locationtxt = new JTextField (5);
         addresstxt = new JTextField (5);
         telnotxt = new JTextField (5);
         paydaytxt = new JTextField (5);
         suppliernamelbl = new JLabel ("Supplier Name");
         namelbl = new JLabel ("Name");
         locationlbl = new JLabel ("Location");
         addresslbl = new JLabel ("Address");
         telnolbl = new JLabel ("TelNo");
         paydaylbl = new JLabel ("Pay Day");
         savebtn = new JButton ("Save");
         mainbtn = new JButton ("Main");
         currentorderlbl = new JLabel ("Current OrderOrder");
         currentordertxt = new JTextField (5);
         deliverydatetxt = new JTextField (5);
         supplieridtxt = new JTextField (5);
         deliverydatelbl = new JLabel ("Delivery Date");
         supplieridlbl = new JLabel ("Supplier ID");
      
      
      
         getContentPane().setBackground(Color.gray);
         setTitle("supplier");
         setSize(624, 537);
         setLayout(null);
      
         
         add (logo);
         add (line1);
         add (line2);
         add (line3);
         add (exitbtn);
         add (logoutbtn);
         add (nametxt);
         add (locationtxt);
         add (addresstxt);
         add (telnotxt);
         add (paydaytxt);
         add (suppliernamelbl);
         add (namelbl);
         add (locationlbl);
         add (addresslbl);
         add (telnolbl);
         add (paydaylbl);
         add (savebtn);
         add (mainbtn);
         add (currentorderlbl);
         add (currentordertxt);
         add (deliverydatetxt);
         add (supplieridtxt);
         add (deliverydatelbl);
         add (supplieridlbl);
      
      
         logo.setBounds (200, 85, 205, 25);
         line1.setBounds (-35, 445, 780, 15);
         line2.setBounds (-100, 55, 765, 25);
         line3.setBounds (-75, 110, 765, 25);
         exitbtn.setBounds (445, 465, 75, 20);
         logoutbtn.setBounds (120, 465, 75, 20);
         nametxt.setBounds (120, 180, 145, 20);
         locationtxt.setBounds (120, 220, 145, 20);
         addresstxt.setBounds (120, 260, 145, 20);
         telnotxt.setBounds (120, 300, 145, 20);
         paydaytxt.setBounds (120, 345, 145, 20);
         suppliernamelbl.setBounds (245, 130, 100, 25);
         namelbl.setBounds (35, 180, 100, 25);
         locationlbl.setBounds (35, 220, 100, 25);
         addresslbl.setBounds (35, 260, 75, 25);
         telnolbl.setBounds (35, 300, 100, 25);
         paydaylbl.setBounds (35, 345, 65, 20);
         savebtn.setBounds (440, 390, 100, 25);
         mainbtn.setBounds (280, 465, 75, 20);
         currentorderlbl.setBounds (280, 175, 120, 25);
         currentordertxt.setBounds (410, 175, 150, 20);
         deliverydatetxt.setBounds (410, 220, 150, 20);
         supplieridtxt.setBounds (410, 260, 150, 20);
         deliverydatelbl.setBounds (280, 220, 100, 25);
         supplieridlbl.setBounds (275, 255, 100, 25);
      	
         ButtonHandler handler = new ButtonHandler(); 
      			          
         exitbtn.addActionListener( handler ); 
         logoutbtn.addActionListener( handler ); 
         savebtn.addActionListener( handler ); 
         mainbtn.addActionListener( handler ); 
      
      }
       private class ButtonHandler implements ActionListener
      {
      
          public void actionPerformed( ActionEvent e )
         {
                     
            Object source = e.getSource();
         	
            if(source == exitbtn)
            {
               int returnValue = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit the system", 
                  					"Exit Confirmation", JOptionPane.YES_NO_OPTION);
            							
               if(returnValue == JOptionPane.YES_OPTION)
               {
                  System.exit(1);
               }
                              
            }
            else if(source == logoutbtn)
            {
               login frame = new login();
               frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
               frame.setLocationRelativeTo(null);
               frame.toFront();
               frame.setVisible (true);
               frame.setResizable(false);
               dispose();                              
            }
            else if(source == savebtn)
            {
                                              
            }
            else if(source == mainbtn)
            {
               Main frame = new Main ();
               frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
               frame.setLocationRelativeTo(null);
               frame.toFront();
               frame.setVisible (true);
               frame.setResizable(false);
               dispose();                              
            }
         }
      }
       public static void main (String[] args)
      {
         supplier frame = new supplier();
         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         frame.setLocationRelativeTo(null);
         frame.toFront();
         frame.setVisible (true);
         frame.setResizable(false);
      }
   }

As long as an element is visible it cannot be disposed. You would have to hide (setVisible(false)) that frame before you can dispose.

I don't agree with much of what you're doing here though, to tell you the truth. I find extending frmae to be a bad idea. I would build the things as a composite on a JPanel and have a separate control class that creates a Frame and simply swaps out the contentPane with the proper JPanel at the proper time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.