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

  • Home
  • SEARCH
  • 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 9154029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:21:57+00:00 2026-06-17T12:21:57+00:00

I currently have two frames, when you run the application the first JFrame that

  • 0

I currently have two frames, when you run the application the first JFrame that shows is a login, it has two input fields and a button. When the user logs in and is verified, I would like to close the frame and start up the second one.

So, the only thing I can think of doing is doing setVisible(false) for the login frame and setVisible(true) for the Main frame.

Is there a better way to do this, or is that the only way?

  • 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-06-17T12:21:57+00:00Added an answer on June 17, 2026 at 12:21 pm

    Personnally, I would start up your second JFrame immediately and replace your first frame with a modal JDialog which would be owned by the JFrame.

    See also this answer to The Use of Multiple JFrames, Good/Bad Practice?

    Here is a basic demo of what I suggest:

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestLogin {
        private JFrame frame;
        private boolean authenticated;
        private JTextField login;
        private JPasswordField password;
    
        protected void initUI() {
            frame = new JFrame(TestLogin.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
        }
    
        protected void showLoginDialog() {
            authenticated = false;
            final JDialog dialog = new JDialog(frame, "Please provide your credentials");
            dialog.setModal(true);
            JPanel panel = new JPanel(new GridBagLayout());
            JPanel buttonPanel = new JPanel();
            login = new JTextField(40);
            password = new JPasswordField(20);
            JLabel loginLabel = new JLabel("Login:");
            JLabel passwordLabel = new JLabel("Password:");
            JButton ok = new JButton("OK");
            ok.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Here perform authentication and set authentication flag to appropriate value
                    authenticated = true;
                    if (authenticated) {
                        setUpFrame();
                        dialog.dispose();
                    }
                }
            });
            JButton cancel = new JButton("Cancel");
            cancel.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            dialog.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosed(WindowEvent e) {
                    if (!authenticated) {
                        System.exit(0);
                    }
                }
            });
            dialog.getRootPane().setDefaultButton(ok);
            buttonPanel.add(ok);
            buttonPanel.add(cancel);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5, 5, 5, 5);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            panel.add(loginLabel, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1.0;
            panel.add(login, gbc);
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            panel.add(passwordLabel, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1.0;
            panel.add(password, gbc);
            panel.add(buttonPanel, gbc);
            dialog.add(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(frame);
            while (!authenticated) {
                dialog.setVisible(true);
            }
        }
    
        protected void setUpFrame() {
            frame.add(new JLabel("Successfully authenticated"));
            frame.revalidate();
            frame.repaint();
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestLogin testLogin = new TestLogin();
                    testLogin.initUI();
                    testLogin.showLoginDialog();
                }
    
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a dataset which has two columns that I'd like to compare.
I have a GWT application that features two frames (com.google.gwt.user.client.ui.Frame). Via Frame.setUrl(...) I can
I currently have two different models: User and Project . The User model has
I (currently) have two forms, one that needs to call the other. In other
Currently I have two tests that each individually test for a button_click within a
What I want to do is have two 'frames'. Left and Right. At application
Currently, I have two separate tabs: variables and frames, and can only have one
I am currently working on a simple GUI application that rolls two dice. I
I have two JTables that I would like displayed in one window. Currently I
Let's say I have two data frames. Each has a DAY, a MONTH, and

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.