javax.swing
Class JFormattedTextField

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:
Accessible, java.awt.image.ImageObserver, MenuContainer, Scrollable, Serializable, SwingConstants

public class JFormattedTextField
extends JTextField

Safe:

See Also:
Serialized Form

Field Summary
static int COMMIT
          Enabled: Constant identifying that when focus is lost, commitEdit should be invoked.
static int COMMIT_OR_REVERT
          Enabled: Constant identifying that when focus is lost, commitEdit should be invoked.
private static Action[] defaultActions
           
private  DocumentListener documentListener
          Used to set the dirty state.
private  boolean edited
          Indicates the current value has been edited.
private  boolean editValid
          True while the value being edited is valid.
private  javax.swing.JFormattedTextField.AbstractFormatterFactory factory
          Factory used to obtain an instance of AbstractFormatter.
private  int focusLostBehavior
          Behavior when focus is lost.
private  javax.swing.JFormattedTextField.AbstractFormatter format
          Object responsible for formatting the current value.
private  Object mask
          Masked used to set the AbstractFormatterFactory.
static int PERSIST
          Enabled: Constant identifying that when focus is lost, the edited value should be left.
static int REVERT
          Enabled: Constant identifying that when focus is lost, editing value should be reverted to current value set on the JFormattedTextField.
private  ActionMap textFormatterActionMap
          ActionMap that the TextFormatter Actions are added to.
private static String uiClassID
           
private  Object value
          Last valid value.
 
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
_bounds, accessibleContext, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
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
 
Constructor Summary
JFormattedTextField()
          Enabled: Creates a JFormattedTextField with no AbstractFormatterFactory.
JFormattedTextField(java.text.Format format)
          Enabled: Creates a JFormattedTextField.
JFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatter formatter)
          Suppressed: Creates a JFormattedTextField with the specified AbstractFormatter.
JFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory)
          Suppressed: Creates a JFormattedTextField with the specified AbstractFormatterFactory.
JFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
          Suppressed: Creates a JFormattedTextField with the specified AbstractFormatterFactory and initial value.
JFormattedTextField(Object value)
          Enabled: Creates a JFormattedTextField with the specified value.
 
Method Summary
 void commitEdit()
          Enabled: Forces the current value to be taken from the AbstractFormatter and set as the current value.
 Action[] getActions()
          Suppressed: Fetches the command list for the editor.
private  javax.swing.JFormattedTextField.AbstractFormatterFactory getDefaultFormatterFactory(Object type)
          Returns an AbstractFormatterFactory suitable for the passed in Object type.
 int getFocusLostBehavior()
          Enabled: Returns the behavior when focus is lost.
 javax.swing.JFormattedTextField.AbstractFormatter getFormatter()
          Suppressed: Returns the AbstractFormatter that is used to format and parse the current value.
 javax.swing.JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
          Suppressed: Returns the current AbstractFormatterFactory.
 String getUIClassID()
          Suppressed: Gets the class ID for a UI.
 Object getValue()
          Enabled: Returns the last valid value.
protected  void invalidEdit()
          Invoked when the user inputs an invalid value.
private  boolean isEdited()
          Returns true if the receiver has been edited.
 boolean isEditValid()
          Enabled: Returns true if the current value being edited is valid.
protected  void processFocusEvent(FocusEvent e)
          Processes any focus events, such as FocusEvent.FOCUS_GAINED or FocusEvent.FOCUS_LOST.
 void setDocument(Document doc)
          Enabled: Associates the editor with a text document.
private  void setEdited(boolean edited)
          Sets the edited state of the receiver.
private  void setEditValid(boolean isValid)
          Sets the validity of the edit on the receiver.
 void setFocusLostBehavior(int behavior)
          Enabled: Sets the behavior when focus is lost.
protected  void setFormatter(javax.swing.JFormattedTextField.AbstractFormatter format)
          Sets the current AbstractFormatter.
private  void setFormatterActions(Action[] actions)
          Resets the Actions that come from the TextFormatter to actions.
 void setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory tf)
          Suppressed: Sets the AbstractFormatterFactory.
 void setValue(Object value)
          Enabled: Sets the value that will be formatted by an AbstractFormatter obtained from the current AbstractFormatterFactory.
private  void setValue(Object value, boolean createFormat)
          Does the setting of the value and firing the event.
private  void writeObject(ObjectOutputStream s)
          See readObject and writeObject in JComponent for more information about serialization in Swing.
 
Methods inherited from class javax.swing.JTextField
addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, hasActionListener, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setFont, setHorizontalAlignment, setScrollOffset
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, 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
_paintImmediately, addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, alwaysOnTop, checkIfChildObscuredBySibling, componentInputMapChanged, computeVisibleRect, computeVisibleRect, compWriteObjectNotify, contains, createToolTip, disable, enable, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getManagingFocusBackwardTraversalKeys, getManagingFocusForwardTraversalKeys, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getWriteObjCounter, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, setWriteObjCounter, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, get, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, iterate, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, size, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, toString, transferFocus, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

uiClassID

private static final String uiClassID

defaultActions

private static final Action[] defaultActions

COMMIT

public static final int COMMIT
Enabled: 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)

COMMIT_OR_REVERT

public static final int COMMIT_OR_REVERT
Enabled: 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)

REVERT

public static final int REVERT
Enabled: Constant identifying that when focus is lost, editing value should be reverted to current value set on the JFormattedTextField.

See Also:
setFocusLostBehavior(int)

PERSIST

public static final int PERSIST
Enabled: Constant identifying that when focus is lost, the edited value should be left.

See Also:
setFocusLostBehavior(int)

factory

private javax.swing.JFormattedTextField.AbstractFormatterFactory factory
Factory used to obtain an instance of AbstractFormatter.


format

private javax.swing.JFormattedTextField.AbstractFormatter format
Object responsible for formatting the current value.


value

private Object value
Last valid value.


editValid

private boolean editValid
True while the value being edited is valid.


focusLostBehavior

private int focusLostBehavior
Behavior when focus is lost.


edited

private boolean edited
Indicates the current value has been edited.


documentListener

private DocumentListener documentListener
Used to set the dirty state.


mask

private Object mask
Masked used to set the AbstractFormatterFactory.


textFormatterActionMap

private ActionMap textFormatterActionMap
ActionMap that the TextFormatter Actions are added to.

Constructor Detail

JFormattedTextField

public JFormattedTextField()
Enabled: Creates a JFormattedTextField with no AbstractFormatterFactory. Use setMask or setFormatterFactory to configure the JFormattedTextField to edit a particular type of value.


JFormattedTextField

public JFormattedTextField(Object value)
Enabled: 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(java.text.Format format)
Enabled: 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(javax.swing.JFormattedTextField.AbstractFormatter formatter)
Suppressed: Creates a JFormattedTextField with the specified AbstractFormatter. The AbstractFormatter is placed in an AbstractFormatterFactory.

Parameters:
formatter - AbstractFormatter to use for formatting.

JFormattedTextField

public JFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory)
Suppressed: Creates a JFormattedTextField with the specified AbstractFormatterFactory.

Parameters:
factory - AbstractFormatterFactory used for formatting.

JFormattedTextField

public JFormattedTextField(javax.swing.JFormattedTextField.AbstractFormatterFactory factory,
                           Object currentValue)
Suppressed: Creates a JFormattedTextField with the specified AbstractFormatterFactory and initial value.

Parameters:
factory - AbstractFormatterFactory used for formatting.
currentValue - Initial value to use
Method Detail

setFocusLostBehavior

public void setFocusLostBehavior(int behavior)
Enabled: Sets the behavior when focus is lost. This will be one of JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.REVERT, JFormattedTextField.COMMIT or JFormattedTextField.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 is JFormattedTextField.COMMIT_OR_REVERT.

Parameters:
behavior - Identifies behavior when focus is lost
Throws:
IllegalArgumentException - if behavior is not one of the known values

getFocusLostBehavior

public int getFocusLostBehavior()
Enabled: Returns the behavior when focus is lost. This will be one of COMMIT_OR_REVERT, COMMIT, REVERT or PERSIST 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(javax.swing.JFormattedTextField.AbstractFormatterFactory tf)
Suppressed: 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) an AbstractFormatterFactory and consequently an AbstractFormatter will be used based on the Class 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 javax.swing.JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
Suppressed: Returns the current AbstractFormatterFactory.

Returns:
AbstractFormatterFactory used to determine AbstractFormatters
See Also:
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)

setFormatter

protected void setFormatter(javax.swing.JFormattedTextField.AbstractFormatter format)
Sets the current AbstractFormatter.

You should not normally invoke this, instead set the AbstractFormatterFactory 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 the AbstractFormatter obtained from the AbstractFormatterFactory.

This is a JavaBeans bound property.

Parameters:
format - AbstractFormatter to use for formatting
See Also:
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)

getFormatter

public javax.swing.JFormattedTextField.AbstractFormatter getFormatter()
Suppressed: Returns the AbstractFormatter that is used to format and parse the current value.

Returns:
AbstractFormatter used for formatting

setValue

public void setValue(Object value)
Enabled: Sets the value that will be formatted by an AbstractFormatter obtained from the current AbstractFormatterFactory. If no AbstractFormatterFactory 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.


getValue

public Object getValue()
Enabled: 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 invoking commitEdit followed by getValue.

Returns:
Last valid value

commitEdit

public void commitEdit()
                throws java.text.ParseException
Enabled: Forces the current value to be taken from the AbstractFormatter and set as the current value. This has no effect if there is no current AbstractFormatter installed.

Throws:
ParseException - if the AbstractFormatter is not able to format the current value

setEditValid

private void setEditValid(boolean isValid)
Sets the validity of the edit on the receiver. You should not normally invoke this. This will be invoked by the AbstractFormatter as the user edits the value.

Not all formatters will allow the component to get into an invalid state, and thus this may never be invoked.

Based on the look and feel this may visually change the state of the receiver.

Parameters:
isValid - boolean indicating if the currently edited value is valid.

isEditValid

public boolean isEditValid()
Enabled: 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.


processFocusEvent

protected void processFocusEvent(FocusEvent e)
Processes any focus events, such as FocusEvent.FOCUS_GAINED or FocusEvent.FOCUS_LOST.

Overrides:
processFocusEvent in class Component
Parameters:
e - the FocusEvent
See Also:
FocusEvent

getActions

public Action[] getActions()
Suppressed: 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 in class JTextField
Returns:
the command list

getUIClassID

public String getUIClassID()
Suppressed: Gets the class ID for a UI.

Overrides:
getUIClassID in class JTextField
Returns:
the string "FormattedTextFieldUI"
See Also:
JComponent.getUIClassID()

setDocument

public void setDocument(Document doc)
Enabled: 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 in class JTextField
Parameters:
doc - the document to display/edit
See Also:
JTextComponent.getDocument()

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
Description copied from class: JTextField
See readObject and writeObject in JComponent for more information about serialization in Swing.

Parameters:
s - the ObjectOutputStream in which to write
IOException

setFormatterActions

private void setFormatterActions(Action[] actions)
Resets the Actions that come from the TextFormatter to actions.


setValue

private void setValue(Object value,
                      boolean createFormat)
Does the setting of the value and firing the event. If createFormat is true, this will also obtain a new AbstractFormatter from the current factory.


setEdited

private void setEdited(boolean edited)
Sets the edited state of the receiver.


isEdited

private boolean isEdited()
Returns true if the receiver has been edited.


getDefaultFormatterFactory

private javax.swing.JFormattedTextField.AbstractFormatterFactory getDefaultFormatterFactory(Object type)
Returns an AbstractFormatterFactory suitable for the passed in Object type.



comments?