Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3604738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:02:09+00:00 2026-05-18T21:02:09+00:00

I am new to Java Swing. I am creating a frame with some components.

  • 0

I am new to Java Swing. I am creating a frame with some components.

I have to close the frame and open another frame when the button is clicked. I had tried setVisible(false) but it only hides the frame, not closing it. When I use System.exit(0), it closed all the frames.

I had tried in another way, i.e. add all the components to panel, and add the panel at first. When the frame has to close I just remove those components in actionListener and add the other components for the corresponding next process.

My entire code is as follows:

package JavaApp;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.IllegalBlockSizeException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Login extends JFrame {

    public static JLabel labelUser;
    public static JLabel labelPass;
    public static JTextField textFieldUser;
    public static JPasswordField passwordField;
    public static JButton clear;
    public static JButton buttonLogin;
    public static JButton ChangePassword;
    public static JButton MasterKey;
    public static JButton AppKey;
    public static JPanel panelGnereate;
    public static JPanel panelLogin;
    public static JFrame frame;
    public static JLabel UserName;
    public static JTextField UserTxt;
    public static JButton GenerateKey;
    public static JPanel panelMaster;

    private static void designUI() {

        frame = new JFrame("Instalation");

        frame.setLayout(new GridLayout(2, 1));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panelLogin = new JPanel();
        panelLogin.setLayout(null);
        labelUser = new JLabel("Enter UserName");
        textFieldUser = new JTextField(10);
        labelPass = new JLabel("Enter Password");
        passwordField = new JPasswordField(10);
        buttonLogin = new JButton("Login");
        clear = new JButton("Cancel");
        panelLogin.add(buttonLogin);
        panelLogin.add(clear);
        panelLogin.add(labelUser);
        panelLogin.add(textFieldUser);
        panelLogin.add(labelPass);
        panelLogin.add(passwordField);
        textFieldUser.setBounds(200, 120, 100, 20);
        labelUser.setBounds(50, 120, 120, 20);
        labelPass.setBounds(50, 150, 120, 20);
        passwordField.setBounds(200, 150, 100, 20);
        buttonLogin.setBounds(70, 180, 100, 20);
        clear.setBounds(200, 180, 100, 20);
        buttonLogin.addActionListener(actionEvent);
        clear.addActionListener(actionEvent);
        frame.add(panelLogin);
        //Login frame ends...

        //Gnereate frame starts here..

        panelGnereate = new JPanel();
        panelGnereate.setLayout(null);
        ChangePassword = new JButton("Change Password");
        MasterKey = new JButton("Create Master Key");
        AppKey = new JButton("Create Application key");
        panelGnereate.add(ChangePassword);
        panelGnereate.add(MasterKey);
        panelGnereate.add(AppKey);
        ChangePassword.setBounds(150, 100, 200, 25);
        MasterKey.setBounds(150, 150, 200, 25);
        AppKey.setBounds(150, 200, 200, 25);

        ChangePassword.addActionListener(actionEvent);
        MasterKey.addActionListener(actionEvent);
        AppKey.addActionListener(actionEvent);
        //Gnerate Frame ends here..


        //MasterKey Generate Starts Here..

        panelMaster = new JPanel();
        panelMaster.setLayout(null);
        UserName = new JLabel("Text");
        UserTxt = new JTextField(20);
        GenerateKey = new JButton("Generate Key");
        panelMaster.add(UserName);
        panelMaster.add(UserTxt);
        panelMaster.add(GenerateKey);
        UserName.setBounds(150, 150, 50, 25);
        UserTxt.setBounds(220, 150, 100, 25);
        GenerateKey.setBounds(180, 180, 150, 25);
        GenerateKey.addActionListener(actionEvent);


        frame.setSize(500, 500);
        frame.setVisible(true);
    }


    public static void main(String args[]) {

         designUI();

    }
    private static ActionListener actionEvent = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String action = e.getActionCommand();
            if ("Login".equals(action)) {
                System.out.println("Login action" + e.getActionCommand());
                if ("".equals(textFieldUser.getText()) || "".equals(passwordField.getText())) {
                    JOptionPane.showMessageDialog(null,
                            "Enter Both UserName and Password");

                } else if ("admin".equals(textFieldUser.getText()) && "admin".equals(passwordField.getText())) {


                    System.out.println("login value" + textFieldUser.getText() + "     " + passwordField.getText());
                    frame.remove(panelLogin);
                    frame.add(panelGnereate);
                    frame.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Wrong Password");

                }
            } else if ("Cancel".equals(action)) {
                System.out.println(e.getActionCommand());
                System.exit(0);

            } else if ("Change Password".equals(action)) {

            } else if ("Create Master Key".equals(action)) {
                frame.remove(panelGnereate);
               frame.add(panelMaster);

                frame.setVisible(true);
            } else if ("Create Application key".equals(action)) {
                System.out.println("Message " + UserTxt.getText());

                frame.setVisible(false);
            }else if("Generate Key".equals(action))
            {
                try {
                    new GenerateTxt(UserTxt.getText());
                } catch (NoSuchAlgorithmException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalBlockSizeException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }

                frame.remove(panelMaster);
                frame.add(panelGnereate);
                 frame.setVisible(true);
            }
        }
        ;

    };
}

It’s not working for throughout the process. When I click the master key it showing relevant textbox after clicking genearteKey it showing same panel not the previous one. But when I am moving the mouse over there it is showing the components of the both panels.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:02:10+00:00Added an answer on May 18, 2026 at 9:02 pm

    looking at your code, you’ll probably need something like this:

                if(frame == null)
                    frame = new JFrame();
                else {
                    //remove the previous JFrame
                    frame.setVisible(false);
                    frame.dispose();
                    //create a new one
                    frame = new JFrame();
                }
    

    you’ll have to put this inside the actionperformed method so that the frame can be removed..
    btw, it would be a gd idea to change your class variables from public to private, in accordance with good programming practice..

    EDIT: To answer the “flickering” problem.
    you’re accessing the swing component from outside the event thread. swing widgets arent generally thread safe. as such, u need to modify your main method:

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                designUI();
            }
        });
    }
    

    also, refer to Swing component flickering when updated a lot for further queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each
I'm new in netbeans and java swing, but also confused. I put some JLabel's
I am new to Java Swing and I am creating a window which displays
Hi I have been learning Java Swing for creating a chess game to practice
I'm new to Java and Java Swing, and I'm creating an application that requires
I am new to Java Swing I want to develop an POS application which
I am new to java Swing and I need a tool to develop attractive
I'm new to Java Swing applications and I'd like to port an old WindowsForm
When creating a new Java project in IntelliJ IDEA, the following directories and files
I have this kind of progamming task without JavaFx, instead it's Java Swing. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.