I need to feed test data to a Swing interval over a time period. The data set is long to parse, so I'm using a SwingWorker
to parse it in the background. When it comes to feeding the data to the GUI (say, one item per second), I could start a Timer
from the SwingWorker
, but this seems overkill. Any reasons not to sleep from within the SwingWorker
itself?
Cheers
Since SwingWorker
doesn't run on the EDT, sleeping within is perfectly fine.
SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing.
In my opinion, a Timer
in addition to a SwingWorker
would be overkill. Instead, just publish the parsed test data whenever possible, even at irregular intervals.
While you make the SwingWorker sleep it will not be loading data. If that is desired then this approach is fine.
If you want to keep loading the data and just pace the feeding to the GUI then it seems like a better design would be to start a Timer in the GUI that would check and pull new data from the worker at an interval, instead of the worker having to make invokeLater
calls back to the GUI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With