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
ModifierConstructorDescriptionprotected
JInputValidator
(JComponent component) Create a JInputValidator with the default preferences.protected
JInputValidator
(JComponent component, boolean onInput, boolean isVerifying) Create a JInputValidator with the default preferences.protected
JInputValidator
(JComponent component, boolean onInput, boolean isVerifying, JInputValidatorPreferences preferences) Create a JInputValidator with custom preferences. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add aPropertyChangeListener
for all properties.void
addPropertyChangeListener
(String propertyName, PropertyChangeListener listener) Add aPropertyChangeListener
for the named property.protected JComponent
Get theJComponent
this validator modifies.final Validation
Create a Validation with typeValidation.Type.NONE
and with the contents ofgetToolTipText()
as the message.Get thePropertyChangeListener
s for all properties.getPropertyChangeListeners
(String propertyName) Get thePropertyChangeListener
s for the named property.protected PropertyChangeSupport
Get thePropertyChangeSupport
supporting this validator.Get the tool tip text used when the validation state isValidation.Type.NONE
.final Validation
Get the current validation.protected abstract Validation
getValidation
(JComponent input, JInputValidatorPreferences preferences) Get the validation object for the current state of the input component.boolean
Check if this validator is verifying whenverify(javax.swing.JComponent)
is called.void
Remove aPropertyChangeListener
for all properties.void
removePropertyChangeListener
(String propertyName, PropertyChangeListener listener) Remove aPropertyChangeListener
for the named property.void
setToolTipText
(String toolTipText) Set the tool tip text used when the validation state isValidation.Type.NONE
.void
setVerifying
(boolean isVerifying) Set if this validator is verifying whenverify(javax.swing.JComponent)
is called.protected final String
trimHtmlTags
(String input) Trims input and removes the leading<html>
and trailing</html>
markers if present.boolean
verify
(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
-true
if validator to validate on all input;false
to validate only on focus change; note this has no effect if component is not aJTextComponent
isVerifying
-true
if validator is to return true or false perverify(javax.swing.JComponent)
;false
to always returntrue
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 nullonInput
-true
if validator to validate on all input;false
to validate only on focus change; note this has no effect if component is not aJTextComponent
isVerifying
-true
if validator is to return true or false perverify(javax.swing.JComponent)
;false
to always returntrue
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 whenverify(javax.swing.JComponent)
is called. When not verifyingverify(javax.swing.JComponent)
always returnstrue
. When verifying, that method returnsfalse
if the currentValidation.Type
isValidation.Type.WARNING
orValidation.Type.DANGER
.- Parameters:
isVerifying
-true
if verifying;false
if not
-
isVerifying
public boolean isVerifying()Check if this validator is verifying whenverify(javax.swing.JComponent)
is called.- Returns:
true
if verifying;false
if 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:
verify
in classInputVerifier
- Returns:
false
ifValidation.getType()
equalsValidation.Type.DANGER
orValidation.Type.WARNING
andisVerifying
istrue
; otherwise returnstrue
-
getComponent
Get theJComponent
this validator modifies.- Returns:
- the validated component
-
getPropertyChangeSupport
Get thePropertyChangeSupport
supporting this validator.- Returns:
- the support
-
trimHtmlTags
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
Create a Validation with typeValidation.Type.NONE
and 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 aPropertyChangeListener
for all properties.- Parameters:
listener
- the listener to add
-
addPropertyChangeListener
Add aPropertyChangeListener
for the named property.- Parameters:
propertyName
- the property to listen tolistener
- the listener to add
-
removePropertyChangeListener
Remove aPropertyChangeListener
for all properties.- Parameters:
listener
- the listener to remove
-
removePropertyChangeListener
Remove aPropertyChangeListener
for the named property.- Parameters:
propertyName
- the property listened tolistener
- the listener to remove
-
getPropertyChangeListeners
Get thePropertyChangeListener
s for all properties.- Returns:
- the listeners
-
getPropertyChangeListeners
Get thePropertyChangeListener
s for the named property.- Parameters:
propertyName
- the property listened to- Returns:
- the listeners
-