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 9109763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:07:16+00:00 2026-06-17T03:07:16+00:00

I am working on a java application which has a login form in a

  • 0

I am working on a java application which has a login form in a jframe. I have text fields and buttons in it.

The login button has an eventlistener which is an inner-class of the class that creates the login window. When user presses the login button, the listener takes the values form the fields and passes it to a validator which validates it using a mysql database and returns true and false based on the input by user. Now based on the return value the listener updates the ui using the if-else statement. This whole thing is working is fine.

The problem is that when the validation is being carried out the gui cannot be used, because every thing is being done with a single thread. So for that time the gui is kind of freezed.
How can I use multithreading to avoid this problem and use other gui components while validation is carried out.

  • 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-17T03:07:18+00:00Added an answer on June 17, 2026 at 3:07 am

    As you’re probably aware, you should never perform long running tasks within the Event Dispatching Thread, this makes you program look like its hung.

    Equally, you should never create/modify any UI component outside the Event Dispatching Thread.

    One of the simplest solutions would be to use a SwingWorker. This allows you execute code in a background thread, but it will automatically resync it’s results back the Event Dispatching Thread…

    public class LoginForm {
    
        public static void main(String[] args) {
            new LoginForm();
        }
    
        public LoginForm() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JDialog frame = new JDialog((JFrame) null, "Login", true);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new LoginPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    System.exit(0);
                }
            });
        }
    
        public class LoginPane extends JPanel {
    
            private JTextField userNameField;
            private JPasswordField passwordField;
            private JButton okay;
            private JButton cancel;
    
            public LoginPane() {
    
                setLayout(new BorderLayout());
    
                userNameField = new JTextField(15);
                passwordField = new JPasswordField(10);
    
                okay = new JButton("Login");
                cancel = new JButton("Cancel");
    
                JPanel mainPane = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.EAST;
                gbc.insets = new Insets(2, 2, 2, 2);
                mainPane.add(new JLabel("User Name:"), gbc);
                gbc.gridy++;
                mainPane.add(new JLabel("Password:"), gbc);
    
                gbc.gridx++;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.WEST;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                mainPane.add(userNameField, gbc);
                gbc.gridy++;
                mainPane.add(passwordField, gbc);
                mainPane.setBorder(new EmptyBorder(8, 8, 8, 8));
    
                add(mainPane);
    
                JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
                buttonPane.setBorder(new EmptyBorder(8, 8, 8, 8));
                buttonPane.add(okay);
                buttonPane.add(cancel);
    
                add(buttonPane, BorderLayout.SOUTH);
    
                okay.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        userNameField.setEnabled(false);
                        passwordField.setEnabled(false);
                        okay.setEnabled(false);
                        cancel.setEnabled(false);
                        new LoginWorker(userNameField.getText(), passwordField.getPassword()).execute();
                    }
                });
    
                cancel.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SwingUtilities.getWindowAncestor(LoginPane.this).dispose();
                    }
                });
            }
    
            public class LoginWorker extends SwingWorker<Boolean, Boolean> {
    
                private String userName;
                private char[] password;
    
                public LoginWorker(String userName, char[] password) {
                    this.userName = userName;
                    this.password = password;
                }
    
                @Override
                protected Boolean doInBackground() throws Exception {
                    // Do you background work here, query the database, compare the values...
                    Thread.sleep(2000);
                    return Math.round((Math.random() * 1)) == 0 ? new Boolean(true) : new Boolean(false);
                }
    
                @Override
                protected void done() {
                    System.out.println("Done...");
                    try {
                        if (get()) {
                            JOptionPane.showMessageDialog(LoginPane.this, "Login sucessful");
                        } else {
                            JOptionPane.showMessageDialog(LoginPane.this, "Login failed");
                        }
                        userNameField.setEnabled(true);
                        passwordField.setEnabled(true);
                        okay.setEnabled(true);
                        cancel.setEnabled(true);
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                }
    
            }
    
        }
    }
    

    Take a look at Concurrency in Swing for more information, in particular, SwingWorker

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

Sidebar

Related Questions

I have a calculator application in java swing, which is working properly with mouse
I am working on a java application which has a lot of use cases.
I am currently working on an Android application which has an order form. I
I am working on a Java application which has a threading issue. While using
I'm working on a Java 6 application server which has a web service for
I'm working on a application which has Java web interface hosted on Glassfish server
I'm working on a Java EE (which I'm fairly new to) web application (JSF,
I'm working on a Swing application (currently running on Java 1.6 update 11) which
So I am working on a java application, and the customer has requested the
Am working on an application which has a requirement to download bulk data from

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.