final class RegexInputVerifier extends InputVerifier
JTextComponent
versus a
regular expression.
This class is likely useful for a wide number of simple input needs.
See the Pattern
class for details regarding regular
expressions.
The main(java.lang.String...)
method is provided as a developer tool for
testing regular expressions versus user input, but the principal use of this
class is to be passed to JComponent.setInputVerifier(javax.swing.InputVerifier)
.
Upon detection of invalid input, this class takes the following actions :
The user of this class is encouraged to always place conditions on data entry in the tooltip for the corresponding field.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
RegexInputVerifier.UseToolTip
Enumeration compels the caller to use a style which reads clearly.
|
Modifier and Type | Field and Description |
---|---|
(package private) static String |
ERROR_MESSAGE_START
The text which begins all error messages.
|
(package private) static RegexInputVerifier |
FLOATING_POINT
Convenience object for input of decimals numbers, eg -23.23321, 100.25.
|
(package private) static RegexInputVerifier |
INTEGER
Convenience object for input of integers: ...-2,-1,0,1,2...
|
(package private) static RegexInputVerifier |
MONEY
Convenience object for input of money values, eg -23, 100.25.
|
(package private) static RegexInputVerifier |
NON_NEGATIVE_FLOATING_POINT
Convenience object for input of non-negative decimals numbers, eg 23.23321, 100.25.
|
(package private) static RegexInputVerifier |
NON_NEGATIVE_INTEGER
Convenience object for input of these integers: 0,1,2...
|
(package private) static RegexInputVerifier |
NON_NEGATIVE_MONEY
Convenience object for input of non-negative money values, eg 23, 100.25.
|
(package private) static RegexInputVerifier |
TEXT
Convenience object for input of short amounts of text.
|
Constructor and Description |
---|
RegexInputVerifier(Pattern aPattern,
RegexInputVerifier.UseToolTip aUseToolTip)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
main(String... aArgs)
Developer test harness for verifying a regular expression, using a simple
graphical interface and a RegexInputVerifier.
|
boolean |
shouldYieldFocus(JComponent aComponent)
Always returns true, in this implementation, such that focus can
always transfer to another component whenever the validation fails.
|
boolean |
verify(JComponent aComponent)
Return true only if the untrimmed user input matches the
regular expression provided to the constructor.
|
static final String ERROR_MESSAGE_START
static final RegexInputVerifier INTEGER
From 1 to 9 digits, possibly preceded by a minus sign. Corresponds approximately to the spec of Integer.parseInt. The limit on the number of digits is related to size of Integer.MAX_VALUE and Integer.MIN_VALUE.
static final RegexInputVerifier NON_NEGATIVE_INTEGER
As in INTEGER
, but with no leading minus sign.
static final RegexInputVerifier TEXT
Text contains from 1 to 75 non-whitespace characters.
static final RegexInputVerifier FLOATING_POINT
Possible leading minus sign, 1 to 10 digits before the decimal, and 1 to 10 digits after the decimal.
static final RegexInputVerifier NON_NEGATIVE_FLOATING_POINT
As in FLOATING_POINT
, but no leading minus sign.
static final RegexInputVerifier MONEY
Possible leading minus sign, from 1 to 15 leading digits, and optionally a decimal place and two decimals.
static final RegexInputVerifier NON_NEGATIVE_MONEY
As in MONEY
, except no leading minus sign
RegexInputVerifier(Pattern aPattern, RegexInputVerifier.UseToolTip aUseToolTip)
aPattern
- regular expression against which all user input will
be verified; aPattern.pattern satisfies
Util.textHasContent(java.lang.String)
.aUseToolTip
- indicates if the tooltip text should be appended to
error messages displayed to the user.public boolean shouldYieldFocus(JComponent aComponent)
If super.shouldYieldFocus returns false, then notify the user of an error.
shouldYieldFocus
in class InputVerifier
aComponent
- is a JTextComponent.public boolean verify(JComponent aComponent)
verify
in class InputVerifier
aComponent
- must be a JTextComponent.public static void main(String... aArgs)
Use of the GUI is straightforward :
(For running this test harness, RegexInputVerifier needs to be a public class; after testing is finished, it is probably a good idea to change the scope to package-private, since the services of this class are used only by the user interface layer.)
Copyright Hirondelle Systems - License - Generated 2013Sep20.10.58