|
Mp3dings 20100427 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.swing.InputVerifier
de.mospace.swing.text.GeneralInputVerifier
public class GeneralInputVerifier
An input verifier that checks if a text component's text can be interpreted as a value of a particular class and optionally if it falls into an allowed range of values of that class.
Here is some sample code
static GeneralInputVerifier giv;
static JTextComponent text = new javax.swing.JTextField();
private static void test(String s){
text.setText(s);
System.out.println(s + ", " + giv.verify(text) + ", " + giv.explain());
}
public static void main(String[] argv) throws Exception{
System.out.println("Double, > -1, < 10");
giv = new GeneralInputVerifier(Double.class, new Double(-1), new Double(10));
test("-2");
test("-1");
test("5");
test("10");
test("11");
test("x12");
System.out.println();
System.out.println("Date, yyyy-MM-dd, > 2007-02-15" );
java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
giv = new GeneralInputVerifier(java.util.Date.class,
java.text.DateFormat.class.getMethod("parse", new Class[]{String.class}),
format);
giv.setLowerLimit(format.parse("2007-02-15"));
test("2007-08-15");
test("200a-08-15");
test("2005-08-15");
}
The output is
Double, > -1, < 10 -2, false, null -1, true, null 5, true, null 10, true, null 11, false, null x12, false, java.lang.NumberFormatException: For input string: "x12" Date, yyyy-MM-dd, > 2007-02-15 2007-08-15, true, null 200a-08-15, false, java.text.ParseException: Unparseable date: "200a-08-15" 2005-08-15, false, null
| Constructor Summary | |
|---|---|
GeneralInputVerifier(Class c)
Constructs a new GeneralInputVerifier for the full range of values in the specified class. |
|
GeneralInputVerifier(Class c,
Comparable min,
Comparable max)
Constructs a new GeneralInputVerifier for the given class and the given limits. |
|
GeneralInputVerifier(Class c,
Method m)
Creates a new GeneralInputVerifier for the specified class that uses the specified static method to construct a value from a string. |
|
GeneralInputVerifier(Class c,
Method m,
Object o)
Creates a new GeneralInputVerifier for the specified class that uses the specified method and object to construct a value from a string. |
|
| Method Summary | |
|---|---|
Throwable |
explain()
Explains why the last call to verify
returned false. |
Class |
getInputClass()
Returns the class against which input will be checked. |
Comparable |
getLowerLimit()
Returns the smallest allowed value. |
Comparable |
getUpperLimit()
Returns the largest allowed value. |
void |
setInputClass(Class c)
Sets the class against which input will be checked. |
void |
setInputClass(Class c,
Method m)
Sets the class against which input will be checked and the static method used to construct values of this class from a string. |
void |
setInputClass(Class c,
Method m,
Object o)
Sets the class against which input will be checked and the method and object used to construct values of this class from a string. |
void |
setLowerLimit(Comparable min)
Sets the smallest allowed value. |
void |
setUpperLimit(Comparable max)
Sets the largest allowed value. |
boolean |
verify(JComponent c)
Verifies the current text of a JTextComponent. |
boolean |
verify(String text)
Verifies the specified text. |
| Methods inherited from class javax.swing.InputVerifier |
|---|
shouldYieldFocus |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public GeneralInputVerifier(Class c,
Comparable min,
Comparable max)
c - the class that input will be interpreted asmin - lower limit. null for none.max - upper limit. null for none.
IllegalArgumentException - if c does not have a
public static valueOf(String) method or a
public constructor with a single string argument.
ClassCastException - if min and max are
neither null nor instances of c.public GeneralInputVerifier(Class c)
c - the class that input will be interpreted as
IllegalArgumentException - if c does not have a
public static
valueOf(String) method or a public constructor with
a single String argument
public GeneralInputVerifier(Class c,
Method m)
c - the class that input will be interpreted asm - a public static method that accepts a single
string argument and whose return type can be assigned to c
IllegalArgumentException - if method
is not a public static method that accepts a single
string argument and has a return type of c
public GeneralInputVerifier(Class c,
Method m,
Object o)
c - the class that input will be interpreted asm - a method that accepts a single
string argument and whose return type can be assigned to co - the object to invoke the method on, or null
if m is static
IllegalArgumentException - if method
is not a public method of o that accepts a single
string argument and has a return type of c| Method Detail |
|---|
public Class getInputClass()
setInputClass(java.lang.Class)public void setInputClass(Class c)
c - the class that input will be interpreted as
IllegalArgumentException - if c does not have a public
static
valueOf(String) method that returns a value of c
or a public constructor with a single string argument.
public void setInputClass(Class c,
Method m)
c - the class that input will be interpreted asm - a static method that accepts a single
string argument and whose return type can be assigned to c
IllegalArgumentException - if method
is not a public static method that accepts a single
string argument and has a return type of c
public void setInputClass(Class c,
Method m,
Object o)
c - the class that input will be interpreted asm - a method that accepts a single
string argument and whose return type can be assigned to co - the object to invoke the method on, or null
if m is static
IllegalArgumentException - if method
is not a public method of o that accepts a single
string argument and has a return type of cpublic void setUpperLimit(Comparable max)
max - upper limit. null for none.
ClassCastException - if max is neither null
nor an instance of getInputClass.public Comparable getUpperLimit()
public void setLowerLimit(Comparable min)
min - lower limit. null for none.
ClassCastException - if min is neither null
nor an instance of getInputClass.public Throwable explain()
verify
returned false. This method will return
any exception thrown within the verify methods.
nullpublic Comparable getLowerLimit()
public boolean verify(JComponent c)
JTextComponent.
verify in class InputVerifierc - a swing text component
c is legal input
ClassCastException - if c is not a
JTextComponentpublic boolean verify(String text)
text - the input to verify
text is legal input.
|
Mp3dings 20100427 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||