Class JInputValidator

java.lang.Object
javax.swing.InputVerifier
com.github.rhwood.jinputvalidator.JInputValidator
Direct Known Subclasses:
PredicateValidator, VerifyingValidator

public abstract class JInputValidator extends InputVerifier
JInputValidator is an InputVerifier that adds a colored icon and tool tip text indicating validation status to the component being verified.
Author:
Randall Wood
See Also:
  • Constructor Details

    • JInputValidator

      protected JInputValidator(JComponent component)
      Create a JInputValidator with the default preferences. The validator created with this call listens to all changes to the component if the component is a JTextComponent.
      Parameters:
      component - the component to attach the validator to; must not be null
    • JInputValidator

      protected JInputValidator(JComponent component, boolean onInput, boolean isVerifying)
      Create a JInputValidator with the default preferences.
      Parameters:
      component - the component to attach the validator to; must not be null
      onInput - true if validator to validate on all input; false to validate only on focus change; note this has no effect if component is not a JTextComponent
      isVerifying - true if validator is to return true or false per verify(javax.swing.JComponent); false to always return true for that method.
    • JInputValidator

      protected JInputValidator(JComponent component, boolean onInput, boolean isVerifying, JInputValidatorPreferences preferences)
      Create a JInputValidator with custom preferences.
      Parameters:
      component - the component to attach the validator to; must not be null
      onInput - true if validator to validate on all input; false to validate only on focus change; note this has no effect if component is not a JTextComponent
      isVerifying - true if validator is to return true or false per verify(javax.swing.JComponent); false to always return true for that method.
      preferences - the custom preferences; must not be null
  • Method Details

    • setVerifying

      public void setVerifying(boolean isVerifying)
      Set if this validator is verifying when verify(javax.swing.JComponent) is called. When not verifying verify(javax.swing.JComponent) always returns true. When verifying, that method returns false if the current Validation.Type is Validation.Type.WARNING or Validation.Type.DANGER.
      Parameters:
      isVerifying - true if verifying; false if not
    • isVerifying

      public boolean isVerifying()
      Check if this validator is verifying when verify(javax.swing.JComponent) is called.
      Returns:
      true if verifying; false if not
    • setToolTipText

      public void setToolTipText(String toolTipText)
      Set the tool tip text used when the validation state is Validation.Type.NONE. If the validation state is NONE when calling this method, the component's tool tip text is changed as well.
      Parameters:
      toolTipText - the default tool tip text for the component being validated
    • getToolTipText

      public String getToolTipText()
      Get the tool tip text used when the validation state is Validation.Type.NONE.
      Returns:
      the tool tip text
    • getValidation

      public final Validation getValidation()
      Get the current validation. This property can be listened to using the property name validation.
      Returns:
      the current validation; will not be null
    • getValidation

      protected abstract Validation getValidation(JComponent input, JInputValidatorPreferences preferences)
      Get the validation object for the current state of the input component.
      Parameters:
      input - the component to get the state of
      preferences - preferences to use for creating the validation
      Returns:
      the validation for the current state; must never be null
    • verify

      public boolean verify(JComponent input)
      This implementation, besides verifying if focus can change, redraws the component with a re-evaluated validation state. The validation state can be retrieved afterwards using getValidation().
      Specified by:
      verify in class InputVerifier
      Returns:
      false if Validation.getType() equals Validation.Type.DANGER or Validation.Type.WARNING and isVerifying is true; otherwise returns true
    • getComponent

      protected JComponent getComponent()
      Get the JComponent this validator modifies.
      Returns:
      the validated component
    • getPropertyChangeSupport

      protected PropertyChangeSupport getPropertyChangeSupport()
      Get the PropertyChangeSupport supporting this validator.
      Returns:
      the support
    • trimHtmlTags

      protected final String trimHtmlTags(String input)
      Trims input and removes the leading <html> and trailing </html> markers if present.
      Parameters:
      input - the input to modify
      Returns:
      the modified input or an empty string if input was null
    • getNoneValidation

      public final Validation getNoneValidation()
      Create a Validation with type Validation.Type.NONE and with the contents of getToolTipText() as the message. Note that the message of a Validation with Type.NONE is not used internally, but this allows a listener to get a Validation with the default tool tip text.
      Returns:
      a new Validation with Type.NONE
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener listener)
      Add a PropertyChangeListener for all properties.
      Parameters:
      listener - the listener to add
    • addPropertyChangeListener

      public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
      Add a PropertyChangeListener for the named property.
      Parameters:
      propertyName - the property to listen to
      listener - the listener to add
    • removePropertyChangeListener

      public void removePropertyChangeListener(PropertyChangeListener listener)
      Remove a PropertyChangeListener for all properties.
      Parameters:
      listener - the listener to remove
    • removePropertyChangeListener

      public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
      Remove a PropertyChangeListener for the named property.
      Parameters:
      propertyName - the property listened to
      listener - the listener to remove
    • getPropertyChangeListeners

      public PropertyChangeListener[] getPropertyChangeListeners()
      Get the PropertyChangeListeners for all properties.
      Returns:
      the listeners
    • getPropertyChangeListeners

      public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
      Get the PropertyChangeListeners for the named property.
      Parameters:
      propertyName - the property listened to
      Returns:
      the listeners