org.jdesktop.swing.animation.timing.sources
Class SwingTimerTimingSource

java.lang.Object
  extended by org.jdesktop.core.animation.timing.TimingSource
      extended by org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource

@ThreadSafe
public final class SwingTimerTimingSource
extends org.jdesktop.core.animation.timing.TimingSource

A timing source based upon a Swing Timer and the SwingUtilities.invokeLater(Runnable) method. This implementation ensures that calls to registered TickListener and PostTickListener objects are always made within the thread context of the Swing EDT.

A typical use, where tl is a TickListener object, would be

 TimingSource ts = new SwingTimerTimingSource(15, TimeUnit.MILLISECONDS);
 ts.init(); // starts the timer
 
 ts.addTickListener(tl); // tl gets tick notifications in the Swing EDT
 
 ts.removeTickListener(tl); // tl stops getting notifications
 
 ts.dispose(); // done using ts
 
If you are not sure what period to set, use the SwingTimerTimingSource() constructor which uses a reasonable default value of 15 milliseconds.

Tasks submitted to TimingSource.submit(Runnable) and calls to registered TickListener and PostTickListener objects from this timing source are always made in the context of the Swing EDT.

Author:
Tim Halloran

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jdesktop.core.animation.timing.TimingSource
org.jdesktop.core.animation.timing.TimingSource.PostTickListener, org.jdesktop.core.animation.timing.TimingSource.TickListener
 
Constructor Summary
SwingTimerTimingSource()
          Constructs a new instance with a period of 15 milliseconds.
SwingTimerTimingSource(long period, TimeUnit unit)
          Constructs a new instance.
 
Method Summary
 void dispose()
           
 void init()
           
 String toString()
           
 
Methods inherited from class org.jdesktop.core.animation.timing.TimingSource
addPostTickListener, addTickListener, getPerTickTask, removePostTickListener, removeTickListener, submit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SwingTimerTimingSource

public SwingTimerTimingSource(long period,
                              TimeUnit unit)
Constructs a new instance. The init() must be called on the new instance to start the timer. The dispose() method should be called to stop the timer.

The Swing timer requires a period of at least 1 millisecond. If the period passed is smaller, it is set to 1 millisecond. It is also not recommended to pass a period much larger than a day as this could lead to integer overflow in the case that the value in milliseconds cannot be represented as an int to be passed to the Swing Timer.

Parameters:
period - the period of time between "tick" events.
unit - the time unit of period parameter.

SwingTimerTimingSource

public SwingTimerTimingSource()
Constructs a new instance with a period of 15 milliseconds. The init() must be called on the new instance to start the timer. The dispose() method should be called to stop the timer.

Method Detail

init

public void init()
Specified by:
init in class org.jdesktop.core.animation.timing.TimingSource

dispose

public void dispose()
Specified by:
dispose in class org.jdesktop.core.animation.timing.TimingSource

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2012. All Rights Reserved.