Link to home
Start Free TrialLog in
Avatar of sharath123in
sharath123in

asked on

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();

                        }

                  }
            }

}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
call dispose() after opening the new window

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