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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:06:39+00:00 2026-05-11T16:06:39+00:00

Related to my previous question: Call repaint from another class in Java? I’m new

  • 0

Related to my previous question: Call repaint from another class in Java?

I’m new to Java and I’ve had a look at some tutorials on SwingWorker. Yet, I’m unsure how to implement it with the example code I gave in the previous question.

Can anyone please explain how to use SwingWorker with regards to my code snippet and/or point me towards a decent tutorial? I have looked but I’m not sure I understand yet.

  • 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-11T16:06:39+00:00Added an answer on May 11, 2026 at 4:06 pm

    Generally, SwingWorker is used to perform long-running tasks in Swing.

    Running long-running tasks on the Event Dispatch Thread (EDT) can cause the GUI to lock up, so one of the things which were done is to use SwingUtilities.invokeLater and invokeAndWait which keeps the GUI responsive by which prioritizing the other AWT events before running the desired task (in the form of a Runnable).

    However, the problem with SwingUtilities is that it didn’t allow returning data from the the executed Runnable to the original method. This is what SwingWorker was designed to address.

    The Java Tutorial has a section on SwingWorker.

    Here’s an example where a SwingWorker is used to execute a time-consuming task on a separate thread, and displays a message box a second later with the answer.

    First off, a class extending SwingWorker will be made:

    class AnswerWorker extends SwingWorker<Integer, Integer>
    {
        protected Integer doInBackground() throws Exception
        {
            // Do a time-consuming task.
            Thread.sleep(1000);
            return 42;
        }
    
        protected void done()
        {
            try
            {
                JOptionPane.showMessageDialog(f, get());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    

    The return type of the doInBackground and get methods are specified as the first type of the SwingWorker, and the second type is the type used to return for the publish and process methods, which are not used in this example.

    Then, in order to invoke the SwingWorker, the execute method is called. In this example, we’ll hook an ActionListener to a JButton to execute the AnswerWorker:

    JButton b = new JButton("Answer!");
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            new AnswerWorker().execute();
        }
    });
    

    The above button can be added to a JFrame, and clicked on to get a message box a second later. The following can be used to initialize the GUI for a Swing application:

    private void makeGUI()
    {
        final JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new FlowLayout());
    
        // include: "class AnswerWorker" code here.
        // include: "JButton" b code here.
    
        f.getContentPane().add(b);
        f.getContentPane().add(new JButton("Nothing"));
        f.pack();
        f.setVisible(true);
    }
    

    Once the application is run, there will be two buttons. One labeled “Answer!” and another “Nothing”. When one clicks on the “Answer!” button, nothing will happen at first, but clicking on the “Nothing” button will work and demonstrate that the GUI is responsive.

    And, one second later, the result of the AnswerWorker will appear in the message box.

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

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you are programmatically making changes to the UITextField once… May 13, 2026 at 2:29 pm
  • Editorial Team
    Editorial Team added an answer Maybe because windows 7 uses .net in some of its… May 13, 2026 at 2:29 pm
  • Editorial Team
    Editorial Team added an answer Strictly speaking it is handled in the IO part of… May 13, 2026 at 2:29 pm

Related Questions

Related to my previous question: Call repaint from another class in Java? I'm new
Very much related to my previous question , but I found this to be
Somewhat related to my question about integers instead of decimals; my vendor provides a
(More package/component install fun for me -- thanks everyone for all your help to
After reading a few related questions I've decided to use the tpl library to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.