Questions tagged [swingworker]

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads. Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker. SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects.

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads. Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker. SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects.

SwingWorker provides a number of communication and control features:

  • The SwingWorker subclass can define a method, done, which is automatically invoked on the event dispatch thread when the background task is finished.
  • SwingWorker implements java.util.concurrent.Future. This interface allows the background task to provide a return value to the other thread. Other methods in this interface allow cancellation of the background task and discovering whether the background task has finished or been cancelled.
  • The background task can provide intermediate results by invoking SwingWorker.publish, causing SwingWorker.process to be invoked from the event dispatch thread.
  • The background task can define bound properties. Changes to these properties trigger events, causing event-handling methods to be invoked on the event dispatch thread.
907 questions
48
votes
2 answers

How do I use SwingWorker in Java?

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…
Relequestual
  • 9,096
  • 6
  • 40
  • 74
32
votes
3 answers

Can a progress bar be used in a class outside main?

Right now, my main just calls a gui with 10 rows. Based on how many of those rows have text, 1 of 9 classes is called (two rows must have text). The called class performs calculations that I'd like to have the progress bar tied to. Here is an…
user568422
  • 361
  • 1
  • 3
  • 7
27
votes
4 answers

Stop/cancel SwingWorker thread?

I'm trying to find out how to stop a SwingWorker thread from running when I press a button. I have been looking around and I'm having some trouble working out how to do this. At the moment this is what I have: new…
user843337
23
votes
7 answers

How should I handle exceptions when using SwingWorker?

I use SwingWorker in Java 6 to avoid running long-running code on the event dispatch thread. If the call to get() in my done() method returns an exception, what is an appropriate way of handling the exception? I'm particularly concerned about…
Steve McLeod
  • 49,211
  • 44
  • 120
  • 177
21
votes
6 answers

SwingWorker: when exactly is called done method?

Javadoc of the done() method of SwingWorker: Executed on the Event Dispatch Thread after the doInBackground method is finished. I've clues that it is not true in the case of canceled worker. Done is called in each case (normal termination or…
AgostinoX
  • 6,923
  • 18
  • 72
  • 120
20
votes
2 answers

Can't get ArrayIndexOutOfBoundsException from Future and SwingWorker if thread starts Executor

I play with multitreading for SwingWorker by using Executor, and I'm there by mistake identified wrong elements from the Vector, looks like as this code pretty ignores that element in Vector doesn't exist my question -> how to/is possible to catch…
mKorbel
  • 108,320
  • 17
  • 126
  • 296
20
votes
2 answers

SwingWorker, done() is executed before process() calls are finished

I have been working with SwingWorkers for a while and have ended up with a strange behavior, at least for me. I clearly understand that due to performance reasons several invocations to publish() method are coallesced in one invocation. It makes…
dic19
  • 17,446
  • 6
  • 35
  • 64
16
votes
3 answers

How can this SwingWorker code be made testable

Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker() { private String location = url.getText(); @Override protected File doInBackground() throws Exception…
Yishai
  • 84,976
  • 26
  • 176
  • 250
16
votes
2 answers

How do I make my SwingWorker example work properly?

I've made my own SwingWorker example to get familiar with how it works. What I'm wanting to do is the following: When the button is clicked I want a progress bar appear until the task is done I want to simply remove the progress bar and add a…
WilliamShatner
  • 926
  • 2
  • 12
  • 25
15
votes
4 answers

Why SwingWorker? Why not just Thread or Runnable?

What are the advantages of using SwingWorker instead of Thread or Runnable?
Rendicahya
  • 3,915
  • 6
  • 34
  • 56
14
votes
5 answers

How to share data with two(2) SwingWorker class in Java

I have two SwingWorker class: FileLineCounterThread and FileDivisionThread I will execute the two threads. When the lines counting thread finishes, it will pass the result to File Division thread. I do not have an idea on how to pass the result to…
eros
  • 4,616
  • 17
  • 48
  • 77
14
votes
3 answers

ability to get the progress on a Future object

With reference to the java.util.concurrent package and the Future interface I notice (unless I am mistaken) that the ability to start a lengthy tasks and be able to query on the progress only comes with the SwingWorker implementing class. This begs…
Marcus Junius Brutus
  • 23,022
  • 30
  • 155
  • 282
13
votes
4 answers

How do I wait for a SwingWorker's doInBackground() method?

Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test { private JDialog window; public Test { // instantiate window } private class Task extends…
Max
  • 1,253
  • 5
  • 16
  • 29
13
votes
1 answer

SwingWorker ProgressBar

I am trying to get a progress bar to accurately reflect my SwingWorker. But I really can't figure out how to do it. I got the bar to just do a static animation until the operation has completed but I want a real active bar. /* * To change this…
Kyle
  • 2,069
  • 9
  • 30
  • 59
13
votes
1 answer

Turn-based Game Design: Event-Driven vs. Game Loop

I am creating my first game in Java. The game is Monopoly. I am struggling with how I should design the game to model its turn-based structure (managing player turns). I want to allow for both a single human-controlled and one or multiple…
nairware
  • 2,776
  • 8
  • 31
  • 56
1
2 3
60 61