net.wotonomy.ui.swing.components
Class KeyDelayTimer

java.lang.Object
  extended by net.wotonomy.ui.swing.components.KeyDelayTimer
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.KeyListener, java.util.EventListener

public class KeyDelayTimer
extends java.lang.Object
implements java.awt.event.ActionListener, java.awt.event.KeyListener

KeyDelayTimer is a utility that listens for KeyEvents from one or more components. After receiving a KeyEvents the timer will broadcast an action event if a specified time interval passes without a subsequent KeyEvent.

This utility is useful for implementing any kind of auto-complete feature in a user interface.

Version:
$Revision: 904 $ $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
Author:
michael@mpowers.net, $Author: cgruber $

Field Summary
protected  java.awt.event.ActionListener actionListener
           
protected  int interval
           
protected  javax.swing.Timer keyTimer
           
protected  java.awt.Component lastFieldTouched
           
protected  long timeLastFieldTouched
           
 
Constructor Summary
KeyDelayTimer()
          Default constructor.
KeyDelayTimer(java.awt.event.ActionListener listener)
          Convenience constructor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Receives ActionEvents from the internal timer.
 void addActionListener(java.awt.event.ActionListener l)
          Adds an action listener to the list that will be notified by button events and changes in button state.
protected  void broadcastEvent(java.awt.event.ActionEvent e)
          Notifies all registered action listeners of a pending Action Event.
 java.awt.Component getComponent()
          Returns the last component that generated a KeyEvent.
 int getInterval()
          Returns the number of milliseconds before an ActionEvent is generated.
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
          Receives key events from one or more components.
 void keyTyped(java.awt.event.KeyEvent e)
           
 void removeActionListener(java.awt.event.ActionListener l)
          Removes an action listener from the list that will be notified by button events and changes in button state.
 void setInterval(int millis)
          Sets the number of milliseconds before an ActionEvent will be generated after a KeyEvent is received.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyTimer

protected javax.swing.Timer keyTimer

lastFieldTouched

protected java.awt.Component lastFieldTouched

timeLastFieldTouched

protected long timeLastFieldTouched

interval

protected int interval

actionListener

protected java.awt.event.ActionListener actionListener
Constructor Detail

KeyDelayTimer

public KeyDelayTimer()
Default constructor.


KeyDelayTimer

public KeyDelayTimer(java.awt.event.ActionListener listener)
Convenience constructor.

Parameters:
listener - An action listener to be notified of delay events.
Method Detail

getComponent

public java.awt.Component getComponent()
Returns the last component that generated a KeyEvent.

Returns:
The component that sent the most recent KeyEvent.

getInterval

public int getInterval()
Returns the number of milliseconds before an ActionEvent is generated. The default is 400.

Returns:
The current delay interval in milliseconds.

setInterval

public void setInterval(int millis)
Sets the number of milliseconds before an ActionEvent will be generated after a KeyEvent is received.

Parameters:
millis - The new delay interval in milliseconds.

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Receives key events from one or more components. Records the component and the time this event was received, then starts the timer.

Specified by:
keyReleased in interface java.awt.event.KeyListener
Parameters:
e - The key event in question.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Receives ActionEvents from the internal timer. If the interval has passed without another KeyEvent, an ActionEvent is broadcast, with the name of this class as the ActionCommand, and the internal timer is stopped.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - The action event in question.

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds an action listener to the list that will be notified by button events and changes in button state.

Parameters:
l - An action listener to be notified.

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes an action listener from the list that will be notified by button events and changes in button state.

Parameters:
l - An action listener to be removed.

broadcastEvent

protected void broadcastEvent(java.awt.event.ActionEvent e)
Notifies all registered action listeners of a pending Action Event.

Parameters:
e - An action event to be broadcast.


Copyright © 2006 null. All Rights Reserved.