|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
com.ccg.swing.TextFieldFormat
public class TextFieldFormat
Class to create text input fields that have a corresponding parser.
Often one needs to gather input from users for Object types which are not strings. For example, if one wants the user to enter a integer number, there is not a each GUI component that can be created to facilitate this.
This class allows one to associate a Format
object with a text field component. In doing so, one gets the
string->object and object->string translation for free.
The easy way to use this class is to see if one of the static creation methods will suit your needs. If the static methods won't work for you, then take the following approach:
formatter/parser that can translate
between your Object type and a string representation.
validate method if you want to check
user input.
setObject method to set values.
getObject method to retreive information.
Format,
Serialized Form| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class javax.swing.JTextField |
|---|
JTextField.AccessibleJTextField |
| Nested classes/interfaces inherited from class javax.swing.text.JTextComponent |
|---|
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding |
| Nested classes/interfaces inherited from class javax.swing.JComponent |
|---|
JComponent.AccessibleJComponent |
| Nested classes/interfaces inherited from class java.awt.Container |
|---|
Container.AccessibleAWTContainer |
| Nested classes/interfaces inherited from class java.awt.Component |
|---|
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| Field Summary |
|---|
| Fields inherited from class javax.swing.JTextField |
|---|
notifyAction |
| Fields inherited from class javax.swing.text.JTextComponent |
|---|
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY |
| Fields inherited from class javax.swing.JComponent |
|---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| Fields inherited from class java.awt.Component |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| Fields inherited from interface javax.swing.SwingConstants |
|---|
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST |
| Fields inherited from interface java.awt.image.ImageObserver |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
|---|---|
TextFieldFormat(Format f)
Construct with a specific Format for translating
between object/string representation. |
|
| Method Summary | |
|---|---|
void |
actionPerformed(ActionEvent e)
Handle the action event (when user presses enter on text field). |
static TextFieldFormat |
createDateEntry(DateFormat df,
Date init,
TimeSpan ts)
Create a date entry field (where user can type strings like: "1963-Dec-25"). |
static TextFieldFormat |
createDoubleEntry(NumberFormat nf,
double init,
double min,
double max)
Create a double entry field (where user can type strings like: "550.2457"). |
static TextFieldFormat |
createIntegerEntry(NumberFormat nf,
int init,
int min,
int max)
Create a integer entry field (where user can type strings like: "550"). |
Format |
getFormat()
Get the format/parse object used to go between a
String and the object's native form. |
Object |
getObject()
Get the Object which this field is used to edit |
Component |
getTableCellRendererComponent(JTable table,
Object val,
boolean isSelected,
boolean hasFocus,
int row,
int col)
Allows component to be used as a JTable cell renderer. |
boolean |
isNullAllowed()
Indicates whether "blank" or null values are permitted. |
void |
setFormat(Format val)
Set the format/parse object used to go between a
String and the object's native form |
void |
setNullAllowed(boolean val)
Set whether "blank" or null values permitted. |
void |
setObject(Object val)
Set the Object which this field is used to edit |
Object |
validateText()
Validate user input WITHOUT updating internal state of object. |
boolean |
validateTextOrFix()
Validate user input and update internal state of object (or "fix" user input). |
boolean |
validateUserInput()
Deprecated. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public TextFieldFormat(Format f)
Format for translating
between object/string representation.
fmt - The format object to use to convert to/from the string
representation.| Method Detail |
|---|
public void actionPerformed(ActionEvent e)
If the user presses the enter key after typing in some text, we validate what they entered - if what is entered is invalid, we silently replace it with the last valid value.
actionPerformed in interface ActionListenerae - ActionEvent which triggered event - we ignore call back if the
source is NOT from this component.validateTextOrFix()
public Object validateText()
throws IllegalStateException
This method verifies that what the user has entered is correct for the format associated with the component. It returns the resulting value (if user entry is valid). It is possible that this method will return null, if you indicate that the field is optional (and the user is allowed to leave it blank).
It should be noted, that this only verifies that the user
input is valid - it doesn't actually update the internal state of
the component. You may want to pass the value returned to the
setObject method if you want to update the
internal state of the object.
This method is provided mainly for the purpose of checking
multiple fields on a panel PRIOR to accepting ANY of the
values. If you simply want to validate and update the internal
state (or possibly fix a bad value), you may want to use the
validateTextOrFix() method.
IllegalStateException - If the text entered is not valid for the format rules.public boolean validateTextOrFix()
This method verifies that what the user has entered is correct
for the format associated with the component. If the user input
is valid, then the internal representation is
updated to correspond to the text entered, reformatted and shown
in the text area. If the user intput is invalid, then the prior
valid value is restored.
public final boolean validateUserInput()
validateTextOrFix or
validateText instead.
validateTextOrFix()public void setFormat(Format val)
format/parse object used to go between a
String and the object's native form
val - New Format value to assign.getFormat()public Format getFormat()
format/parse object used to go between a
String and the object's native form.
setFormat(java.text.Format)
public void setObject(Object val)
throws ParseException
Object which this field is used to edit
val - New Object value to assign.
ParseExceptiongetObject()public Object getObject()
Object which this field is used to edit
setObject(java.lang.Object)
public Component getTableCellRendererComponent(JTable table,
Object val,
boolean isSelected,
boolean hasFocus,
int row,
int col)
JTable cell renderer.
getTableCellRendererComponent in interface TableCellRenderertable - Table the which is to use this object as a renderer.val - The value to put into the component.sel - Whether the component is selected.hasFocus - Whether the component has the focus.row - Row index in table - ignored.col - Column index in table - ignored.
public void setNullAllowed(boolean val)
val - New boolean value to assign.
see #getNullAllowedpublic boolean isNullAllowed()
public static TextFieldFormat createDateEntry(DateFormat df,
Date init,
TimeSpan ts)
df - The DateFormat to use for the parsing date
information (may be null in which case we'll use a ISO looking
version).init - Initial date to start with. May be null in which
case will either use the earlies time in the time span
allowed, or the current time if no time span limit is in
affect.ts - The TimeSpan to enforce. This may be null if
ALL dates are accepted.
TextFieldFormat that can validate a user's input.
public static TextFieldFormat createIntegerEntry(NumberFormat nf,
int init,
int min,
int max)
nf - The NumberFormat to use for the parsing
integer values with (may be null in which case we'll create
what we think is best).init - Initial value to start with.min - The minimim value allowed (use Integer.MIN_VALUE for no minimum).max - The maximim value allowed (use Integer.MAX_VALUE for no minimum). Must be larger or equal to
min.
TextFieldFormat that can validate a user's input.
public static TextFieldFormat createDoubleEntry(NumberFormat nf,
double init,
double min,
double max)
nf - The NumberFormat to use for the parsing
double values with (may be null in which case we'll create
what we think is best - kind of a HP looking form).init - Initial value to start with.min - The minimim value allowed (use Double.MIN_VALUE for no minimum).max - The maximim value allowed (use Double.MAX_VALUE for no minimum). Must be larger or equal to
min.
TextFieldFormat that can validate a user's input.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||