JFormattedTextField (Java 2 Platform SE 5.0) (original) (raw)


javax.swing

Class JFormattedTextField

java.lang.Object extended by java.awt.Component extended by java.awt.Container extended by javax.swing.JComponent extended by javax.swing.text.JTextComponent extended by javax.swing.JTextField extended by javax.swing.JFormattedTextField

All Implemented Interfaces:

ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants


public class JFormattedTextField

extends JTextField

JFormattedTextField extends JTextField adding support for formatting arbitrary values, as well as retrieving a particular object once the user has edited the text. The following illustrates configuring a JFormattedTextField 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 a PropertyChangeListener and listening forPropertyChangeEvents with the property name value.

JFormattedTextField allows configuring what action should be taken when focus is lost. The possible configurations are:

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 theAbstractFormatter 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 like REVERT.
JFormattedTextField.PERSIST Do nothing, don't obtain a newAbstractFormatter, and don't update the value.

The default is JFormattedTextField.COMMIT_OR_REVERT, refer to setFocusLostBehavior(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 an InputVerifier. The following code snippet shows a potential implementation of such an InputVerifier:

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 of JFormattedTextField.AbstractFormatterFactory. Instances of JFormattedTextField.AbstractFormatter are notified when they become active by way of theinstall method, at which point theJFormattedTextField.AbstractFormatter can install whatever it needs to, typically a DocumentFilter. Similarly whenJFormattedTextField no longer needs the AbstractFormatter, it will invokeuninstall.

JFormattedTextField typically queries the AbstractFormatterFactory for anAbstractFormat when it gains or loses focus. Although this can change based on the focus lost policy. If the focus lost policy is JFormattedTextField.PERSIST and the JFormattedTextField has been edited, theAbstractFormatterFactory will not be queried until the value has been commited. Similarly if the focus lost policy isJFormattedTextField.COMMIT and an exception is thrown from stringToValue, theAbstractFormatterFactory will not be querired when focus is lost or gained.

JFormattedTextField.AbstractFormatter is also responsible for determining when values are commited to the JFormattedTextField. SomeJFormattedTextField.AbstractFormatters 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 current JFormattedTextField.AbstractFormatter by way of invoking commitEdit. commitEdit will be invoked whenever return is pressed in theJFormattedTextField.

If an AbstractFormatterFactory has not been explicitly set, one will be set based on the Class of the value type aftersetValue has been invoked (assuming value is non-null). For example, in the following code an appropriateAbstractFormatterFactory and AbstractFormatter will be created to handle formatting of numbers:

JFormattedTextField tf = new JFormattedTextField(); tf.setValue(new Number(100));

Warning: As the AbstractFormatter will typically install a DocumentFilter on theDocument, and a NavigationFilter 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: 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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

Since:

1.4


Nested Class Summary
static class JFormattedTextField.AbstractFormatter Instances of AbstractFormatter 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 of AbstractFormatterFactory are used byJFormattedTextField to obtain instances ofAbstractFormatter which in turn are used to format values.
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
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 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
JFormattedTextField() Creates a JFormattedTextField with noAbstractFormatterFactory.
JFormattedTextField(Format format) Creates a JFormattedTextField.
JFormattedTextField(JFormattedTextField.AbstractFormatter formatter) Creates a JFormattedTextField with the specifiedAbstractFormatter.
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory) Creates a JFormattedTextField with the specifiedAbstractFormatterFactory.
[JFormattedTextField](../../javax/swing/JFormattedTextField.html#JFormattedTextField%28javax.swing.JFormattedTextField.AbstractFormatterFactory, java.lang.Object%29)(JFormattedTextField.AbstractFormatterFactory factory,Object currentValue) Creates a JFormattedTextField with the specifiedAbstractFormatterFactory and initial value.
JFormattedTextField(Object value) Creates a JFormattedTextField with the specified value.
Method Summary
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 the AbstractFormatter that is used to format and parse the current value.
JFormattedTextField.AbstractFormatterFactory getFormatterFactory() Returns the current AbstractFormatterFactory.
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 current AbstractFormatter.
void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf) Sets the AbstractFormatterFactory.
void setValue(Object value) Sets the value that will be formatted by anAbstractFormatter obtained from the currentAbstractFormatterFactory.
Methods inherited from class javax.swing.JTextField
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 inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, [addKeymap](../../javax/swing/text/JTextComponent.html#addKeymap%28java.lang.String, javax.swing.text.Keymap%29), copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, [getScrollableBlockIncrement](../../javax/swing/text/JTextComponent.html#getScrollableBlockIncrement%28java.awt.Rectangle, int, int%29), getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, [getScrollableUnitIncrement](../../javax/swing/text/JTextComponent.html#getScrollableUnitIncrement%28java.awt.Rectangle, int, int%29), getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, [getText](../../javax/swing/text/JTextComponent.html#getText%28int, int%29), getToolTipText, getUI, isEditable, [loadKeymap](../../javax/swing/text/JTextComponent.html#loadKeymap%28javax.swing.text.Keymap, javax.swing.text.JTextComponent.KeyBinding[], javax.swing.Action[]%29), modelToView, moveCaretPosition, paste, [read](../../javax/swing/text/JTextComponent.html#read%28java.io.Reader, java.lang.Object%29), removeCaretListener, removeKeymap, removeNotify, replaceSelection, [select](../../javax/swing/text/JTextComponent.html#select%28int, int%29), selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, [contains](../../javax/swing/JComponent.html#contains%28int, int%29), createToolTip, disable, enable, [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, boolean, boolean%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, char, char%29), [firePropertyChange](../../javax/swing/JComponent.html#firePropertyChange%28java.lang.String, int, int%29), [fireVetoableChange](../../javax/swing/JComponent.html#fireVetoableChange%28java.lang.String, java.lang.Object, java.lang.Object%29), getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, [paintImmediately](../../javax/swing/JComponent.html#paintImmediately%28int, int, int, int%29), paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, [processKeyBinding](../../javax/swing/JComponent.html#processKeyBinding%28javax.swing.KeyStroke, java.awt.event.KeyEvent, int, boolean%29), processKeyEvent, processMouseEvent, processMouseMotionEvent, [putClientProperty](../../javax/swing/JComponent.html#putClientProperty%28java.lang.Object, java.lang.Object%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, javax.swing.KeyStroke, int%29), [registerKeyboardAction](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int%29), removeAncestorListener, removeVetoableChangeListener, [repaint](../../javax/swing/JComponent.html#repaint%28long, int, int, int, int%29), repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, [reshape](../../javax/swing/JComponent.html#reshape%28int, int, int, int%29), revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, [setFocusTraversalKeys](../../javax/swing/JComponent.html#setFocusTraversalKeys%28int, java.util.Set%29), setForeground, setInheritsPopupMenu, [setInputMap](../../javax/swing/JComponent.html#setInputMap%28int, javax.swing.InputMap%29), setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods inherited from class java.awt.Container
add, [add](../../java/awt/Container.html#add%28java.awt.Component, int%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object, int%29), [add](../../java/awt/Container.html#add%28java.lang.String, java.awt.Component%29), addContainerListener, [addImpl](../../java/awt/Container.html#addImpl%28java.awt.Component, java.lang.Object, int%29), addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Container.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, [findComponentAt](../../java/awt/Container.html#findComponentAt%28int, int%29), findComponentAt, getComponent, [getComponentAt](../../java/awt/Container.html#getComponentAt%28int, int%29), getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, [list](../../java/awt/Container.html#list%28java.io.PrintStream, int%29), [list](../../java/awt/Container.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Container.html#locate%28int, int%29), minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, [setComponentZOrder](../../java/awt/Container.html#setComponentZOrder%28java.awt.Component, int%29), setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
[action](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, [gotFocus](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../java/awt/Component.html#inside%28int, int%29), isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), list, list, list, location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, postEvent, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, resize, [resize](../../java/awt/Component.html#resize%28int, int%29), [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setName, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), show, show, size, toString, transferFocus, transferFocusUpCycle
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29)
Field Detail

COMMIT

public static final int COMMIT

Constant identifying that when focus is lost,commitEdit should be invoked. If in commiting the new value a ParseException is thrown, the invalid value will remain.

See Also:

setFocusLostBehavior(int), Constant Field Values


COMMIT_OR_REVERT

public static final int COMMIT_OR_REVERT

Constant identifying that when focus is lost,commitEdit should be invoked. If in commiting the new value a ParseException is thrown, the value will be reverted.

See Also:

setFocusLostBehavior(int), Constant Field Values


REVERT

public static final int REVERT

Constant identifying that when focus is lost, editing value should be reverted to current value set on theJFormattedTextField.

See Also:

setFocusLostBehavior(int), Constant Field Values


PERSIST

public static final int PERSIST

Constant identifying that when focus is lost, the edited value should be left.

See Also:

setFocusLostBehavior(int), Constant Field Values

Constructor Detail

JFormattedTextField

public JFormattedTextField()

Creates a JFormattedTextField with noAbstractFormatterFactory. Use setMask orsetFormatterFactory to configure theJFormattedTextField to edit a particular type of value.


JFormattedTextField

public JFormattedTextField(Object value)

Creates a JFormattedTextField with the specified value. This will create an AbstractFormatterFactory based on the type of value.

Parameters:

value - Initial value for the JFormattedTextField


JFormattedTextField

public JFormattedTextField(Format format)

Creates a JFormattedTextField. format is wrapped in an appropriate AbstractFormatter which is then wrapped in an AbstractFormatterFactory.

Parameters:

format - Format used to look up an AbstractFormatter


JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)

Creates a JFormattedTextField with the specifiedAbstractFormatter. The AbstractFormatter is placed in an AbstractFormatterFactory.

Parameters:

formatter - AbstractFormatter to use for formatting.


JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)

Creates a JFormattedTextField with the specifiedAbstractFormatterFactory.

Parameters:

factory - AbstractFormatterFactory used for formatting.


JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)

Creates a JFormattedTextField with the specifiedAbstractFormatterFactory and initial value.

Parameters:

factory - AbstractFormatterFactory used for formatting.

currentValue - Initial value to use

Method Detail

setFocusLostBehavior

public void setFocusLostBehavior(int behavior)

Sets the behavior when focus is lost. This will be one ofJFormattedTextField.COMMIT_OR_REVERT,JFormattedTextField.REVERT,JFormattedTextField.COMMIT orJFormattedTextField.PERSIST Note that some AbstractFormatters may push changes as they occur, so that the value of this will have no effect.

This will throw an IllegalArgumentException if the object passed in is not one of the afore mentioned values.

The default value of this property isJFormattedTextField.COMMIT_OR_REVERT.

Parameters:

behavior - Identifies behavior when focus is lost

Throws:

[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang") - if behavior is not one of the known values


getFocusLostBehavior

public int getFocusLostBehavior()

Returns the behavior when focus is lost. This will be one ofCOMMIT_OR_REVERT,COMMIT,REVERT orPERSIST Note that some AbstractFormatters may push changes as they occur, so that the value of this will have no effect.

Returns:

returns behavior when focus is lost


setFormatterFactory

public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)

Sets the AbstractFormatterFactory.AbstractFormatterFactory is able to return an instance of AbstractFormatter that is used to format a value for display, as well an enforcing an editing policy.

If you have not explicitly set an AbstractFormatterFactory by way of this method (or a constructor) anAbstractFormatterFactory and consequently anAbstractFormatter will be used based on theClass of the value. NumberFormatter will be used for Numbers, DateFormatter will be used for Dates, otherwise DefaultFormatter will be used.

This is a JavaBeans bound property.

Parameters:

tf - AbstractFormatterFactory used to lookup instances of AbstractFormatter


getFormatterFactory

public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()

Returns the current AbstractFormatterFactory.

Returns:

AbstractFormatterFactory used to determineAbstractFormatters

See Also:

setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)


setFormatter

protected void setFormatter(JFormattedTextField.AbstractFormatter format)

Sets the current AbstractFormatter.

You should not normally invoke this, instead set theAbstractFormatterFactory or set the value.JFormattedTextField will invoke this as the state of the JFormattedTextField changes and requires the value to be reset.JFormattedTextField passes in theAbstractFormatter obtained from theAbstractFormatterFactory.

This is a JavaBeans bound property.

Parameters:

format - AbstractFormatter to use for formatting

See Also:

setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)


getFormatter

public JFormattedTextField.AbstractFormatter getFormatter()

Returns the AbstractFormatter that is used to format and parse the current value.

Returns:

AbstractFormatter used for formatting


setValue

public void setValue(Object value)

Sets the value that will be formatted by anAbstractFormatter obtained from the currentAbstractFormatterFactory. If noAbstractFormatterFactory has been specified, this will attempt to create one based on the type of value.

The default value of this property is null.

This is a JavaBeans bound property.

Parameters:

value - Current value to display


getValue

public Object getValue()

Returns the last valid value. Based on the editing policy of the AbstractFormatter this may not return the current value. The currently edited value can be obtained by invokingcommitEdit followed by getValue.

Returns:

Last valid value


commitEdit

public void commitEdit() throws ParseException

Forces the current value to be taken from theAbstractFormatter and set as the current value. This has no effect if there is no currentAbstractFormatter installed.

Throws:

[ParseException](../../java/text/ParseException.html "class in java.text") - if the AbstractFormatter is not able to format the current value


isEditValid

public boolean isEditValid()

Returns true if the current value being edited is valid. The value of this is managed by the current AbstractFormatter, as such there is no public setter for it.

Returns:

true if the current value being edited is valid.


invalidEdit

protected void invalidEdit()

Invoked when the user inputs an invalid value. This gives the component a chance to provide feedback. The default implementation beeps.


processInputMethodEvent

protected void processInputMethodEvent(InputMethodEvent e)

Processes any input method events, such asInputMethodEvent.INPUT_METHOD_TEXT_CHANGED orInputMethodEvent.CARET_POSITION_CHANGED.

Overrides:

[processInputMethodEvent](../../javax/swing/text/JTextComponent.html#processInputMethodEvent%28java.awt.event.InputMethodEvent%29) in class [JTextComponent](../../javax/swing/text/JTextComponent.html "class in javax.swing.text")

Parameters:

e - the InputMethodEvent

See Also:

InputMethodEvent


processFocusEvent

protected void processFocusEvent(FocusEvent e)

Processes any focus events, such asFocusEvent.FOCUS_GAINED orFocusEvent.FOCUS_LOST.

Overrides:

[processFocusEvent](../../java/awt/Component.html#processFocusEvent%28java.awt.event.FocusEvent%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Parameters:

e - the FocusEvent

See Also:

FocusEvent


getActions

public Action[] getActions()

Fetches the command list for the editor. This is the list of commands supported by the plugged-in UI augmented by the collection of commands that the editor itself supports. These are useful for binding to events, such as in a keymap.

Overrides:

[getActions](../../javax/swing/JTextField.html#getActions%28%29) in class [JTextField](../../javax/swing/JTextField.html "class in javax.swing")

Returns:

the command list


getUIClassID

public String getUIClassID()

Gets the class ID for a UI.

Overrides:

[getUIClassID](../../javax/swing/JTextField.html#getUIClassID%28%29) in class [JTextField](../../javax/swing/JTextField.html "class in javax.swing")

Returns:

the string "FormattedTextFieldUI"

See Also:

JComponent.getUIClassID()


setDocument

public void setDocument(Document doc)

Associates the editor with a text document. The currently registered factory is used to build a view for the document, which gets displayed by the editor after revalidation. A PropertyChange event ("document") is propagated to each listener.

Overrides:

[setDocument](../../javax/swing/JTextField.html#setDocument%28javax.swing.text.Document%29) in class [JTextField](../../javax/swing/JTextField.html "class in javax.swing")

Parameters:

doc - the document to display/edit

See Also:

JTextComponent.getDocument()



Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.