I Am Creating A student database Project in Java Swing . Now i have created many JFrames . Such as if i want to add a new student a new frame will appear which will have some textfields and button , now i want to make disappear the last JFrame . I used dispose() method to close the running JFrame , and used .setVisible(true) for the next frame , and i did the same with the next frame , when the work of the add students frame is over it will return in the old JFrame , I used the same procedure , but turns out the Frame doesnt disappear , Only the first frame after running my program is disappearing , but others are not , This is the First Code
    btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try{
                frame.dispose();
                frame.setVisible(false);
                AddStudent add=new AddStudent();
                add.setVisible(true);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
And the second code is
    btnAddStudent = new JButton("Add Student");
    btnAddStudent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //int action=JOptionPane.showConfirmDialog(null, "Do You   Really Want To Add This Studen?","Delete",JOptionPane.YES_NO_OPTION);
            //if(action==0){
            try{
                frame.dispose();
                frame.setVisible(false);
                Admin admin=new Admin();
                admin.setVisible(true);
            }catch(Exception e){
                e.printStackTrace();
            } 
            //}
        }
    });
Now when i go from the first frame to second frame the first frame disappears , when the work of the second frame is over and i first frame comes , the second frame doesn't disappear . Both Frames Are In different classes. Any Solution ? Sorry For My Bad English
 
    