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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:50:58+00:00 2026-06-18T15:50:58+00:00

I have a method that can take a long time to be executed. Let’s

  • 0

I have a method that can take a long time to be executed. Let’s call it longTime();

This method is called from an action listener on a button.

I want to display a "Please wait.." message while this method is executing.

The problem is that the Jframe doesn’t respond, it seems to be stuck or waiting for something.

Important note: The running time of longTime() can be different. The problem appears only when it takes more than 2-3 seconds.

I tried to do all of these in invokeLate but that didn’t help.

SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    JLabel label = new JLabel("Please wait...");
    label.setFont(new Font("Serif", Font.PLAIN, 25));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.setLocationRelativeTo(null);
    frame.setUndecorated(true);
    frame.pack();
    frame.setAlwaysOnTop(true);
    frame.setVisible(true);
    try{            
      longTime();  //HERE IS THE FUNCTION THAT TAKES A LONG TIME  
    }catch(Exception e){   }
    frame.dispose();  //AFTER THE LONG FUNCTION FINISHES, DISPOSE JFRAME        
  } 
});

Any ideas of how to fix this?

  • 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-18T15:50:59+00:00Added an answer on June 18, 2026 at 3:50 pm

    You have to spawn another thread and run your longTime() task in another thread.
    Calling invokeLater() still runs it in the UI thread, which you don’t want to block.

    Consider following code:

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        JLabel label = new JLabel("Please wait...");
        label.setFont(new Font("Serif", Font.PLAIN, 25));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setUndecorated(true);
        frame.pack();
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
    
        new Thread(new Runnable() {
            @Override
            public void run() {
            try{            
                longTime();  //HERE IS THE FUNCTION THAT TAKES A LONG TIME  
            }catch(Exception e){   }
                frame.dispose();     //AFTER THE LONG FUNCTION FINISHES, DISPOSE JFRAME     
            } 
            }).start();
    

    It’s possible that you want to update a data model from that newly spawned thread. That would be the right place to use SwingUtilities.invokeLater(). Don’t update models in this new thread. Models for UI components must be updated in UI thread only. Invoke later does exactly that.

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

Sidebar

Related Questions

Let's say I have a subroutine/method that a user can call to test some
I have code in my Page_load method that can take up to a few
Okay, so I want to have a generic method that can read data from
Let's say you have a business logic method that can perform some operation across
I can't figure out why this is. I have a connect method that works
I saw DatabaseErrorHandler interface that have onCorruption method , this interface can be passed
I am doing some long simulations that can take from several hours to several
I have a method that will receive a string , but before I can
i have a javascript method that takes a date: convert(new Date(02/20/2010); how can i
I have a Rails controller action to test. In that action, a method User.can?

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.