Class TimeoutWorker
- All Implemented Interfaces:
Runnable, Future<Void>, RunnableFuture<Void>
A SwingWorker that acts as a countdown timer, executing a callback upon completion.
This worker runs a countdown from a specified start time on a background thread.
If the timer completes without being cancelled, it executes a provided Runnable
on the Event Dispatch Thread (EDT).
- Author:
- Lehlogonolo Poole
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class SwingWorker
SwingWorker.StateValueNested classes/interfaces inherited from interface Future
Future.State -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTimeoutWorker(int startTimeInSeconds, Runnable onFinishedCallback) Constructs a new TimeoutWorker. -
Method Summary
Methods inherited from class SwingWorker
addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, process, publish, removePropertyChangeListener, run, setProgressMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Future
exceptionNow, resultNow, state
-
Field Details
-
startTimeInSeconds
private final int startTimeInSeconds -
onFinishedCallback
-
-
Constructor Details
-
TimeoutWorker
-
-
Method Details
-
doInBackground
The main background task that performs the countdown.This method runs on a worker thread. It sleeps for one-second intervals and checks for cancellation on each iteration.
- Specified by:
doInBackgroundin classSwingWorker<Void,Void> - Returns:
- null, as this worker does not produce a final result.
- Throws:
Exception
-
done
protected void done()Called on the Event Dispatch Thread (EDT) after the background task finishes.This method checks if the worker was cancelled. If it was not, it executes the success callback.
- Overrides:
donein classSwingWorker<Void,Void>
-