Javatpoint Logo
Javatpoint Logo

Java SwingWorker Class

Java is a popular object-oriented programming language for creating software for a variety of platforms. Java's capacity for dealing with concurrency, which allows for the simultaneous execution of several processes, is one of its important properties. Using worker threads is one method for achieving concurrency in Java. The Java worker class will be covered in this section, along with its definition, attributes, and Java programming applications.

The SwingWorker class is an extension of the Java worker class. The SwingWorker class is an object that supports background execution of time-consuming, lengthy activities. With the help of the worker class, time-consuming tasks can be completed in such a separate thread without interfering with the user interface. The application shall continue to run normally and the user interface will stay responsive by doing the lengthy operation in a separate thread.

When you need to conduct a lengthy task, like downloading a sizable file from a website or processing a sizable amount of data, the worker class comes in handy. The user interface can stay responsive and the application can still be used while the work is being carried out in the background if the task is carried out in a separate thread.

Java SwingWorker Class Properties

  1. It extends the Swing Worker class.
  2. It executes the long-running task in a separate thread.
  3. It provides support for updating the user interface while the task is running.
  4. It can return a value when the task is completed.

Use of SwingWorker Class

  1. Create a class that extends the SwingWorker class.
  2. Implement the doInBackground() method to perform the long-running task in a separate thread.
  3. Implement the done() method to update the user interface when the task is completed.
  4. Call the execute() method to start the task in a separate thread.

Here is an example program that demonstrates how to use the worker class in Java:

WorkerExample.java

Output:

Task 0 completed
Task 1 completed
Task 2 completed
Task 3 completed
Task 4 completed
Task 5 completed
Task 96 completed
Task 97 completed
Task 98 completed
Task 99 completed

In the above programme, a Swing JFrame is made and equipped with a start button, a text area, and a progress bar. The software builds and runs a worker object when the user presses the start button. The doInBackground() method is overridden by the worker object, which extends the SwingWorker class, to do a time-consuming operation in a different thread. The long-running task in this illustration is a loop that publishes a message to the text area and updates the progress indicator. The user interface can be updated while the task is ongoing by using the publish() method. The publish() method is used to broadcast messages, and the worker thread uses the process() method to update the text area with those messages.

When the task is finished, the done() method is invoked. In this instance, the progress bar is reset to 0 and the start button is enabled once more by the done() method.

Here is an illustration of what the result might resemble:

As the worker job advances, the output will be shown in the GUI window and updated in real-time. The user has further control over the GUI, including the ability to browse across the text area, resize the window, and click the "Start" button once again to launch a new worker job.

The output shows that while the lengthy work is being executed in a different thread, the programme updates the progress bar and text area.

In conclusion, Java's worker class is an effective tool for managing time-consuming tasks in a different thread. You may maintain a responsive user interface while running time-consuming tasks in the background by using the worker class. While the task is executing, the user interface can be updated with the help of the worker class, which is simple to utilise. Use the worker class to enhance the user experience if you have a lengthy task that needs to be completed in your Java application.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA