Navigation to another frame by Buton click

Here is my code after I execute it. It results in a frame with, fist and last names. when I click the login button it opens in a new window. But, the labels from previous frame are there. I want to open a new frame and close old one and those should not appear in the new one.
code..............


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


public class StudentRegister extends JFrame
{
      private JTextField textfirstName,textlastName,textStudentid;
      private JButton buttonSubmit,buttonAdd,buttonDrop,buttonFee,buttonEE,buttonME,buttonCE,buttonIE;
      //status==submit
      private JLabel labelfirstName,labellastName,labelStudentI d;
      public String firstName[] = new String[50];
      public String lastName[] = new String[50];

      public StudentRegister()
      {

      Container container = getContentPane() ;
      container.setLayout(new FlowLayout());

      labelfirstName = new JLabel("FirstName") ;
      container.add(labelfirstName );
      textfirstName = new JTextField(15) ;
      container.add(textfirstName );

      labellastName = new JLabel("LastName ") ;
      container.add(labellastName );
      textlastName = new JTextField(15) ;
      container.add(textlastName );


      buttonSubmit = new JButton("LogIn");
    container.add(buttonSubmit) ;

    ButtonHandler handler = new ButtonHandler();
      buttonSubmit.addActionListener( handler );






      setSize ( 500,600) ;
      setVisible(true);

      }

      public void StudentRegister1()
      {
      Container container1 = getContentPane() ;
      container1.setLayout(new FlowLayout());

      buttonAdd = new JButton("AddCourse");
    container1.add(buttonAdd) ;
    buttonDrop = new JButton("DropCourse");
    container1.add(buttonDrop) ;
    buttonFee = new JButton("ViewFee");
    container1.add(buttonFee) ;
    setSize ( 500,600) ;
      setVisible(true);
      }

      public static void main(String args[])
      {
            StudentRegister student = new StudentRegister() ;
            student.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
      }



            class ButtonHandler implements ActionListener
            {
                  public void actionPerformed(ActionEvent event)
                  {

                        if (event.getSource() == buttonSubmit)
                        {
                        StudentRegister student1 = new StudentRegister() ;
                        student1.StudentRegister1();

                        }

                  }
            }

}
sharath123inAsked:
Who is Participating?
 
CEHJCommented:
Really the best way to do this sort of thing is to use a JTabbePane and use the first tab to login. Proceed to the main tab if successful

http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html
0
 
objectsCommented:
call dispose() after opening the new window

                  if (event.getSource() == buttonSubmit)
                  {
                    StudentRegister student1 = new StudentRegister() ;
                    student1.StudentRegister1();
                    dispose();
                  }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.