com.objectwave.viewUtility
Class TextFieldChangeListener

java.lang.Object
  |
  +--com.objectwave.viewUtility.TextFieldChangeListener
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener, java.awt.event.FocusListener

public abstract class TextFieldChangeListener
extends java.lang.Object
implements java.awt.event.FocusListener, java.awt.event.ActionListener

This class with call it's onChange() method whenever the given text field's value changes. This is useful for triggering other actions (populating other controls based on the change, for instance), or for executing validations.

This class is intended to be initialized a little differently than is normally done, as it is self-registering. Instead of passing an instance of this class to a JTextField's addXxxListener() method, create an instance of this listener and pass the JTextField in the constructor. The constructor will register whatever is necessary.

There is no need to maintain a local reference to an instance of this listener since the JTextField will refer to it, thereby preventing garbage collection (at least until the JTextField is collected).

Example:

   // ...
   JTextField tf = new JTextField();
   // ...
   new TextFieldChangeListener(tf)
     {
       public abtract void onChange(String oldText, String newText)
       {
         System.out.println("Changed from '" + oldText + "' to '" + newText + "'");
       }
     };
   // ...
 

Version:
1.0
Author:
Steven Sinclair

Field Summary
protected  boolean alreadyHandled
           
protected  java.lang.String currentText
           
protected  javax.swing.JTextField textField
           
 
Constructor Summary
TextFieldChangeListener(javax.swing.JTextField newTextField)
          Construct an instance of this class with the given text field.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          When the control performs and action, and the event has not been handled, call the onChange method.
 void focusGained(java.awt.event.FocusEvent e)
          When the control gains focus, 'remember' the current field text.
 void focusLost(java.awt.event.FocusEvent e)
          When the control loses focus, and the event has not been handled, call the onChange method.
 javax.swing.JTextField getTextField()
           
protected  boolean isAlreadyHandled()
           
abstract  void onChange(java.lang.String oldText, java.lang.String newText)
          Abstract method which must be implemented to define what action should be taken when the field focus changes.
protected  void setTextField(javax.swing.JTextField newTextField)
          Set the text field and register self as focus and action listener.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

textField

protected javax.swing.JTextField textField

currentText

protected java.lang.String currentText

alreadyHandled

protected boolean alreadyHandled
Constructor Detail

TextFieldChangeListener

public TextFieldChangeListener(javax.swing.JTextField newTextField)
Construct an instance of this class with the given text field.

Parameters:
newTextField -
Method Detail

setTextField

protected void setTextField(javax.swing.JTextField newTextField)
Set the text field and register self as focus and action listener.

Parameters:
newTextField - The new TextField value

getTextField

public javax.swing.JTextField getTextField()
Returns:
JTextField - the text field associated with this listener

isAlreadyHandled

protected boolean isAlreadyHandled()
Returns:
boolean - true if this change has already been handled. Otherwise, set internal flag to true and return false.

focusGained

public void focusGained(java.awt.event.FocusEvent e)
When the control gains focus, 'remember' the current field text. Called by the control as part of the FocusListener interface.

Specified by:
focusGained in interface java.awt.event.FocusListener
Parameters:
e -

focusLost

public void focusLost(java.awt.event.FocusEvent e)
When the control loses focus, and the event has not been handled, call the onChange method. Called by the control as part of the FocusListener interface.

Specified by:
focusLost in interface java.awt.event.FocusListener
Parameters:
e -

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
When the control performs and action, and the event has not been handled, call the onChange method.

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

onChange

public abstract void onChange(java.lang.String oldText,
                              java.lang.String newText)
Abstract method which must be implemented to define what action should be taken when the field focus changes.

Parameters:
oldText -
newText -