Interface DelayedAction


public interface DelayedAction
Represents an action that executes after a specified delay, with the ability to cancel it before execution.

This interface is primarily used to prevent UI flicker when showing transient progress indicators. For example, when a refresh operation might complete quickly, delaying the display of a progress bar by a few hundred milliseconds prevents flickering when the operation finishes almost immediately.

The action is scheduled on the Event Dispatch Thread using a Swing Timer and executes exactly once if not cancelled. If the operation completes before the delay expires, calling cancel() prevents the action from executing.

Example usage:


 DelayedAction showProgress = delayedAction(300, () -> {
     progressBar.setVisible(true);
 });

 // Later, if operation completes quickly:
 showProgress.cancel();
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cancels the delayed action, preventing it from executing if the delay has not yet elapsed.
    delayedAction(int delay, Runnable action)
    Creates a new delayed action that executes the given action after the specified delay.
  • Method Details

    • cancel

      void cancel()
      Cancels the delayed action, preventing it from executing if the delay has not yet elapsed. If the action has already executed, this method has no effect. This method is safe to call multiple times.
    • delayedAction

      static DelayedAction delayedAction(int delay, Runnable action)
      Creates a new delayed action that executes the given action after the specified delay. The action is scheduled on the Event Dispatch Thread and executes exactly once if not cancelled.
      Parameters:
      delay - the delay in milliseconds before the action executes
      action - the action to execute after the delay
      Returns:
      a new DelayedAction instance that can be cancelled
      Throws:
      NullPointerException - if action is null