-2

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

CodeHead
  • 177
  • 1
  • 2
  • 12
  • 4
    An application should have a single main JFrame which should always be visible. If you need a child window, for example to create a Student, then you should create a `modal JDialog` to create the Student and enter the Student information. When the dialog closes you are than back on your main frame ready for further processing. See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – camickr Jul 11 '15 at 18:26
  • Have a look at http://stackoverflow.com/questions/7256606/jdialog-setvisiblefalse-vs-dispose for `dispose` vs `setVisible` – Eric Leibenguth Jul 12 '15 at 15:13

1 Answers1

0

My personal experience tells me it´s not a good idea to have more than one JFrame "at the same level".

You should create one JFrame as your "Main", and create others when needed without disposing the "Main" JFrame.

I´m not sure if i am explaining that well, english is not my first language.

The idea is basically not to dispose the current JFrame when creating another one.

Mayuso
  • 1,291
  • 4
  • 19
  • 41