- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.text.JTextComponent
-
- javax.swing.JTextField
-
- javax.swing.JFormattedTextField
-
- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
,Scrollable
,SwingConstants
@JavaBean public class JFormattedTextField extends JTextField
JFormattedTextField
extendsJTextField
adding support for formatting arbitrary values, as well as retrieving a particular object once the user has edited the text. The following illustrates configuring aJFormattedTextField
to edit dates:JFormattedTextField ftf = new JFormattedTextField(); ftf.setValue(new Date());
Once a
JFormattedTextField
has been created, you can listen for editing changes by way of adding aPropertyChangeListener
and listening forPropertyChangeEvent
s with the property namevalue
.JFormattedTextField
allows configuring what action should be taken when focus is lost. The possible configurations are:Possible JFormattedTextField configurations and their descriptions Value Description JFormattedTextField.REVERT Revert the display to match that of getValue
, possibly losing the current edit.JFormattedTextField.COMMIT Commits the current value. If the value being edited isn't considered a legal value by the AbstractFormatter
that is, aParseException
is thrown, then the value will not change, and then edited value will persist.JFormattedTextField.COMMIT_OR_REVERT Similar to COMMIT
, but if the value isn't legal, behave likeREVERT
.JFormattedTextField.PERSIST Do nothing, don't obtain a new AbstractFormatter
, and don't update the value.JFormattedTextField.COMMIT_OR_REVERT
, refer tosetFocusLostBehavior(int)
for more information on this.JFormattedTextField
allows the focus to leave, even if the currently edited value is invalid. To lock the focus down while theJFormattedTextField
is an invalid edit state you can attach anInputVerifier
. The following code snippet shows a potential implementation of such anInputVerifier
:public class FormattedTextFieldVerifier extends InputVerifier { public boolean verify(JComponent input) { if (input instanceof JFormattedTextField) { JFormattedTextField ftf = (JFormattedTextField)input; AbstractFormatter formatter = ftf.getFormatter(); if (formatter != null) { String text = ftf.getText(); try { formatter.stringToValue(text); return true; } catch (ParseException pe) { return false; } } } return true; } public boolean shouldYieldFocus(JComponent input) { return verify(input); } }
Alternatively, you could invoke
commitEdit
, which would also commit the value.JFormattedTextField
does not do the formatting it self, rather formatting is done through an instance ofJFormattedTextField.AbstractFormatter
which is obtained from an instance ofJFormattedTextField.AbstractFormatterFactory
. Instances ofJFormattedTextField.AbstractFormatter
are notified when they become active by way of theinstall
method, at which point theJFormattedTextField.AbstractFormatter
can install whatever it needs to, typically aDocumentFilter
. Similarly whenJFormattedTextField
no longer needs theAbstractFormatter
, it will invokeuninstall
.JFormattedTextField
typically queries theAbstractFormatterFactory
for anAbstractFormat
when it gains or loses focus. Although this can change based on the focus lost policy. If the focus lost policy isJFormattedTextField.PERSIST
and theJFormattedTextField
has been edited, theAbstractFormatterFactory
will not be queried until the value has been committed. Similarly if the focus lost policy isJFormattedTextField.COMMIT
and an exception is thrown fromstringToValue
, theAbstractFormatterFactory
will not be queried when focus is lost or gained.JFormattedTextField.AbstractFormatter
is also responsible for determining when values are committed to theJFormattedTextField
. SomeJFormattedTextField.AbstractFormatter
s will make new values available on every edit, and others will never commit the value. You can force the current value to be obtained from the currentJFormattedTextField.AbstractFormatter
by way of invokingcommitEdit
.commitEdit
will be invoked whenever return is pressed in theJFormattedTextField
.If an
AbstractFormatterFactory
has not been explicitly set, one will be set based on theClass
of the value type aftersetValue
has been invoked (assuming value is non-null). For example, in the following code an appropriateAbstractFormatterFactory
andAbstractFormatter
will be created to handle formatting of numbers:JFormattedTextField tf = new JFormattedTextField(); tf.setValue(100);
Warning: As the
AbstractFormatter
will typically install aDocumentFilter
on theDocument
, and aNavigationFilter
on theJFormattedTextField
you should not install your own. If you do, you are likely to see odd behavior in that the editing policy of theAbstractFormatter
will not be enforced.Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the
java.beans
package. Please seeXMLEncoder
.- Since:
- 1.4
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JFormattedTextField.AbstractFormatter
Instances ofAbstractFormatter
are used byJFormattedTextField
to handle the conversion both from an Object to a String, and back from a String to an Object.static class
JFormattedTextField.AbstractFormatterFactory
Instances ofAbstractFormatterFactory
are used byJFormattedTextField
to obtain instances ofAbstractFormatter
which in turn are used to format values.-
Nested classes/interfaces declared in class javax.swing.JTextField
JTextField.AccessibleJTextField
-
Nested classes/interfaces declared in class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
-
Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
Fields Modifier and Type Field Description static int
COMMIT
Constant identifying that when focus is lost,commitEdit
should be invoked.static int
COMMIT_OR_REVERT
Constant identifying that when focus is lost,commitEdit
should be invoked.static int
PERSIST
Constant identifying that when focus is lost, the edited value should be left.static int
REVERT
Constant identifying that when focus is lost, editing value should be reverted to current value set on theJFormattedTextField
.-
Fields declared in class javax.swing.JTextField
notifyAction
-
Fields declared in class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
-
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Fields declared in 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
-
-
Constructor Summary
Constructors Constructor Description JFormattedTextField()
Creates aJFormattedTextField
with noAbstractFormatterFactory
.JFormattedTextField(Object value)
Creates a JFormattedTextField with the specified value.JFormattedTextField(Format format)
Creates aJFormattedTextField
.JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
Creates aJFormattedTextField
with the specifiedAbstractFormatter
.JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
Creates aJFormattedTextField
with the specifiedAbstractFormatterFactory
.JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
Creates aJFormattedTextField
with the specifiedAbstractFormatterFactory
and initial value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
commitEdit()
Forces the current value to be taken from theAbstractFormatter
and set as the current value.Action[]
getActions()
Fetches the command list for the editor.int
getFocusLostBehavior()
Returns the behavior when focus is lost.JFormattedTextField.AbstractFormatter
getFormatter()
Returns theAbstractFormatter
that is used to format and parse the current value.JFormattedTextField.AbstractFormatterFactory
getFormatterFactory()
Returns the currentAbstractFormatterFactory
.String
getUIClassID()
Gets the class ID for a UI.Object
getValue()
Returns the last valid value.protected void
invalidEdit()
Invoked when the user inputs an invalid value.boolean
isEditValid()
Returns true if the current value being edited is valid.protected void
processFocusEvent(FocusEvent e)
Processes any focus events, such asFocusEvent.FOCUS_GAINED
orFocusEvent.FOCUS_LOST
.protected void
processInputMethodEvent(InputMethodEvent e)
Processes any input method events, such asInputMethodEvent.INPUT_METHOD_TEXT_CHANGED
orInputMethodEvent.CARET_POSITION_CHANGED
.void
setDocument(Document doc)
Associates the editor with a text document.void
setFocusLostBehavior(int behavior)
Sets the behavior when focus is lost.protected void
setFormatter(JFormattedTextField.AbstractFormatter format)
Sets the currentAbstractFormatter
.void
setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
Sets theAbstractFormatterFactory
.void
setValue(Object value)
Sets the value that will be formatted by anAbstractFormatter
obtained from the currentAbstractFormatterFactory
.-
Methods declared in class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setFont, setHorizontalAlignment, setScrollOffset
-
Methods declared in class javax.swing.text.JTextComponent
addCaretListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, replaceSelection,
-
-