net.sf.swinglib.field
Class FieldValidator

java.lang.Object
  extended by net.sf.swinglib.field.FieldValidator

public class FieldValidator
extends Object

Applies a regular expression to a text field to check its validity. Provides an optional listener that will change the field's background color if invalid.

Note: the validator records the component's document at time of construction. If you change the document, you must call reset().


Constructor Summary
FieldValidator(JTextComponent component, String regex)
          Constructs a "listen-only" instance; you must explicitly call isValid() to check the field's state.
FieldValidator(JTextComponent component, String regex, Color highlight)
          Constructs a an instance that will change the field's background color when its contents are invalid, and revert to its parent's color when the contents are valid.
FieldValidator(JTextComponent component, String regex, Color highlight, Color normal)
          Constructs a an instance that will change the field's background color to either a "normal" or a "highlight" color depending on the field's contents.
 
Method Summary
 boolean isValid()
          Determines whether the document is currently valid (note: this may be called within an event handler, as long as the update has been applied to the document).
 void reset()
          Call this method if you change the document on the component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldValidator

public FieldValidator(JTextComponent component,
                      String regex)
Constructs a "listen-only" instance; you must explicitly call isValid() to check the field's state.

Parameters:
component - The component to listen to; the new instance is attached as a DocumentListener.
regex - A regular expression defining the valid contents of the field. This will be applied on any change to the underlying document.

FieldValidator

public FieldValidator(JTextComponent component,
                      String regex,
                      Color highlight)
Constructs a an instance that will change the field's background color when its contents are invalid, and revert to its parent's color when the contents are valid.

Note: the first check is performed by this constructor.

Parameters:
component - The component to listen to; the new instance is attached as a DocumentListener.
regex - A regular expression defining the valid contents of the field. This will be applied on any change to the underlying document.
highlight - When the field is invalid, it will be set to this color.

FieldValidator

public FieldValidator(JTextComponent component,
                      String regex,
                      Color highlight,
                      Color normal)
Constructs a an instance that will change the field's background color to either a "normal" or a "highlight" color depending on the field's contents.

Note: the first check is performed by this constructor. There is no need to make a separate setBackground() call on the component itself.

Parameters:
component - The component to listen to; the new instance is attached as a DocumentListener.
regex - A regular expression defining the valid contents of the field. This will be applied on any change to the underlying document.
highlight - When the field is invalid, it will be set to this color.
normal - When the field is valid, it will be set to this color.
Method Detail

isValid

public boolean isValid()
Determines whether the document is currently valid (note: this may be called within an event handler, as long as the update has been applied to the document).


reset

public void reset()
Call this method if you change the document on the component.