java.lang.Object
javax.swing.InputVerifier
com.github.rhwood.jinputvalidator.JInputValidator
- Direct Known Subclasses:
PredicateValidator,VerifyingValidator
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 Summary
ConstructorsModifierConstructorDescriptionprotectedJInputValidator(JComponent component) Create a JInputValidator with the default preferences.protectedJInputValidator(JComponent component, boolean onInput, boolean isVerifying) Create a JInputValidator with the default preferences.protectedJInputValidator(JComponent component, boolean onInput, boolean isVerifying, JInputValidatorPreferences preferences) Create a JInputValidator with custom preferences. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd aPropertyChangeListenerfor all properties.voidaddPropertyChangeListener(String propertyName, PropertyChangeListener listener) Add aPropertyChangeListenerfor the named property.protected JComponentGet theJComponentthis validator modifies.final ValidationCreate a Validation with typeValidation.Type.NONEand with the contents ofgetToolTipText()as the message.Get thePropertyChangeListeners for all properties.getPropertyChangeListeners(String propertyName) Get thePropertyChangeListeners for the named property.protected PropertyChangeSupportGet thePropertyChangeSupportsupporting this validator.Get the tool tip text used when the validation state isValidation.Type.NONE.final ValidationGet the current validation.protected abstract ValidationgetValidation(JComponent input, JInputValidatorPreferences preferences) Get the validation object for the current state of the input component.booleanCheck if this validator is verifying whenverify(javax.swing.JComponent)is called.voidRemove aPropertyChangeListenerfor all properties.voidremovePropertyChangeListener(String propertyName, PropertyChangeListener listener) Remove aPropertyChangeListenerfor the named property.voidsetToolTipText(String toolTipText) Set the tool tip text used when the validation state isValidation.Type.NONE.voidsetVerifying(boolean isVerifying) Set if this validator is verifying whenverify(javax.swing.JComponent)is called.protected final StringtrimHtmlTags(String input) Trims input and removes the leading and trailing<html>tokens if present.booleanverify(JComponent input) This implementation, besides verifying if focus can change, redraws the component with a re-evaluated validation state.Methods inherited from class javax.swing.InputVerifier
shouldYieldFocus, shouldYieldFocus, verifyTarget
-
Constructor Details
-
JInputValidator
Create a JInputValidator with the default preferences. The validator created with this call listens to all changes to the component if the component is aJTextComponent.- Parameters:
component- the component to attach the validator to; must not be null
-
JInputValidator
Create a JInputValidator with the default preferences.- Parameters:
component- the component to attach the validator to; must not be nullonInput-trueif validator to validate on all input;falseto validate only on focus change; note this has no effect if component is not aJTextComponentisVerifying-trueif validator is to return true or false perverify(javax.swing.JComponent);falseto always returntruefor 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 nullonInput-trueif validator to validate on all input;falseto validate only on focus change; note this has no effect if component is not aJTextComponentisVerifying-trueif validator is to return true or false perverify(javax.swing.JComponent);falseto always returntruefor that method.preferences- the custom preferences; must not be null
-
-
Method Details
-
setVerifying
public void setVerifying(boolean isVerifying) Set if this validator is verifying whenverify(javax.swing.JComponent)is called. When not verifyingverify(javax.swing.JComponent)always returnstrue. When verifying, that method returnsfalseif the currentValidation.TypeisValidation.Type.WARNINGorValidation.Type.DANGER.- Parameters:
isVerifying-trueif verifying;falseif not
-
isVerifying
public boolean isVerifying()Check if this validator is verifying whenverify(javax.swing.JComponent)is called.- Returns:
trueif verifying;falseif not
-
setToolTipText
Set the tool tip text used when the validation state isValidation.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
Get the tool tip text used when the validation state isValidation.Type.NONE.- Returns:
- the tool tip text
-
getValidation
Get the current validation. This property can be listened to using the property namevalidation.- 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 ofpreferences- preferences to use for creating the validation- Returns:
- the validation for the current state; must never be null
-
verify
This implementation, besides verifying if focus can change, redraws the component with a re-evaluated validation state. The validation state can be retrieved afterwards usinggetValidation().- Specified by:
verifyin classInputVerifier- Returns:
falseifValidation.getType()equalsValidation.Type.DANGERorValidation.Type.WARNINGandfIsVerifyingistrue; otherwise returnstrue
-
getComponent
Get theJComponentthis validator modifies.- Returns:
- the validated component
-
getPropertyChangeSupport
Get thePropertyChangeSupportsupporting this validator.- Returns:
- the support
-
trimHtmlTags
Trims input and removes the leading and trailing<html>tokens if present.- Parameters:
input- the input to modify- Returns:
- the modified input or an empty string if input was null
-
getNoneValidation
Create a Validation with typeValidation.Type.NONEand with the contents ofgetToolTipText()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
Add aPropertyChangeListenerfor all properties.- Parameters:
listener- the listener to add
-
addPropertyChangeListener
Add aPropertyChangeListenerfor the named property.- Parameters:
propertyName- the property to listen tolistener- the listener to add
-
removePropertyChangeListener
Remove aPropertyChangeListenerfor all properties.- Parameters:
listener- the listener to remove
-
removePropertyChangeListener
Remove aPropertyChangeListenerfor the named property.- Parameters:
propertyName- the property listened tolistener- the listener to remove
-
getPropertyChangeListeners
Get thePropertyChangeListeners for all properties.- Returns:
- the listeners
-
getPropertyChangeListeners
Get thePropertyChangeListeners for the named property.- Parameters:
propertyName- the property listened to- Returns:
- the listeners
-