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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:01:03+00:00 2026-05-14T15:01:03+00:00

I am using javax.swing.SwingWorker for the first time. I want to update a JLabel

  • 0

I am using javax.swing.SwingWorker for the first time.

I want to update a JLabel from the interim results published by the swing worker as follows:

publish("Published String");

Now to update the JLabel, I have coded the following:

process(List<String> chunks) {
    if (chunks.size() > 0) {
        String text = chunks.get(chunks.size() - 1);
        label.setText(text);
    }
}

The above code works but my problem(or to be more specific, my doubt) is as follows:

The above swing worker task is an annonymous inner class so it can access label field.

But what if I want to make the swing worker class a non-inner class. Should I need to pass label as an argument to the constructor of swing worker class so that the process() method can access.

Or Is there any other way?

What approach does other developer follow to update UI components from the swing worker class’ result when the swing worker class is not an inner class?

  • 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-14T15:01:03+00:00Added an answer on May 14, 2026 at 3:01 pm

    But what if I want to make the swing
    worker class a non-inner class. Should
    I need to pass label as an argument to
    the constructor of swing worker class
    so that the process() method can
    access.

    That’s perfectly fine. From the SwingWorker documentation:

    class PrimeNumbersTask extends 
        SwingWorker<List<Integer>, Integer> {
            PrimeNumbersTask(JTextArea textArea, int numbersToFind) { 
                //initialize 
            }
    
            @Override
            public List<Integer> doInBackground() {
                while (! enough && ! isCancelled()) {
                    number = nextPrimeNumber();
                    publish(number);
                    setProgress(100 * numbers.size() / numbersToFind);
                }
            }
            return numbers;
        }
    
        @Override
        protected void process(List<Integer> chunks) {
            for (int number : chunks) {
                textArea.append(number + "\n");
            }
        }
    }
    
    JTextArea textArea = new JTextArea();
    final JProgressBar progressBar = new JProgressBar(0, 100);
    PrimeNumbersTask task = new PrimeNumbersTask(textArea, N);
    task.addPropertyChangeListener(
        new PropertyChangeListener() {
            public  void propertyChange(PropertyChangeEvent evt) {
                if ("progress".equals(evt.getPropertyName())) {
                    progressBar.setValue((Integer)evt.getNewValue());
                }
            }
        });
    
    task.execute();
    

    Notice the constructor PrimeNumbersTask(JTextArea textArea, int numbersToFind). They pass the JTextArea to update.

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

Sidebar

Related Questions

I'm using the following code with the javax.swing.text.html.parser.ParserDelegator in order to parse hyperlinks from
I am using swing worker to validate files, I want the startLine and endLine
I have class JLabelExtended, which extends class javax.swing.JLabel. I extend it, because I want
I'm using the FlexGantt Javax Swing package ( http://www.dlsc.com/ ) to display some Gantt
I am trying to create a GUI using java swing. From there I have
I'm programming my very first GUI app in Java using the Swing framework. I've
I was wondering, I want to report some form of progress from my swing
Made a custom ListCellRenderer: import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel;
I have a task running using javax.swing.Timer. It executes once every few minutes. The
I am learning Java using Eclipse - started a project with javax.swing and could

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.