Navigation to another frame by Buton click

Posted on 2005-04-28
Last Modified: 2012-06-27
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.

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


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

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



Question by:sharath123in
    LVL 86

    Accepted Solution

    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
    LVL 92

    Expert Comment

    call dispose() after opening the new window

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

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now