public class JFormattedTextField extends JTextField
JFormattedTextField延伸
JTextField添加格式任意值的支持,以及检索一个特定的对象,一旦用户编辑文本。下面的说明配置
JFormattedTextField编辑日期:
jformattedtextfield FTF =新jformattedtextfield();FTF setValue(新());
一旦JFormattedTextField已经创建,你可以聆听编辑修改通过添加一个PropertyChangeListener倾听与属性名称value PropertyChangeEvents。
JFormattedTextField允许配置应采取什么行动的时候失去了焦点。可能的配置:
Value |
描述 |
|---|---|
| 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, a ParseException 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 new AbstractFormatter, and don't update the value. |
JFormattedTextField.COMMIT_OR_REVERT,指
setFocusLostBehavior(int)更多这方面的信息。
JFormattedTextField让焦点离开,即使当前编辑的值无效。要将焦点锁定下来,JFormattedTextField是无效的编辑状态可以附加一个InputVerifier。下面的代码片段显示了这种InputVerifier潜在的实现:
公共课formattedtextfieldverifier延伸inputverifier {公共布尔验证(JComponent输入){如果(输入是jformattedtextfield){jformattedtextfield FTF =(jformattedtextfield)输入;abstractformatter格式化= getformatter() FTF;如果(格式化程序!=零)字符串文字= gettext() FTF;尝试{格式化程序。stringtovalue(文本);返回true;} catch(ParseException PE){返回false;}}}返回true;}公共布尔shouldyieldfocus(JComponent输入){返回验证(输入);}}
或者,你可以调用commitEdit,也承诺的价值。
JFormattedTextField不格式化它的自我,而格式是通过JFormattedTextField.AbstractFormatter是从JFormattedTextField.AbstractFormatterFactory实例实例做了。实例JFormattedTextField.AbstractFormatter通知当他们成为积极的install方法,在这一点上,JFormattedTextField.AbstractFormatter可以安装任何需要,一个典型的DocumentFilter。同样在JFormattedTextField不再需要AbstractFormatter,它将调用uninstall。
JFormattedTextField通常查询一AbstractFormat的AbstractFormatterFactory获得或失去焦点时。虽然这可以改变的基础上,重点丢失的政策。如果焦点失去了政策JFormattedTextField.PERSIST和JFormattedTextField已编辑的AbstractFormatterFactory不会被查询到的价值一直致力。同样,如果失去了政策重点是JFormattedTextField.COMMIT扔从stringToValue是个例外,这AbstractFormatterFactory不会被失去或获得焦点时。
JFormattedTextField.AbstractFormatter还负责确定值时,致力于JFormattedTextField。一些JFormattedTextField.AbstractFormatters将每个编辑可使新的价值观,和其他人将不会承诺的价值。你可以强制电流值是通过调用commitEdit从目前得到的JFormattedTextField.AbstractFormatter。commitEdit将被调用时,返回的是压在JFormattedTextField。
如果一个AbstractFormatterFactory没有显式设置,一个将基于值类型的类后setValue被调用(假设值非空)。例如,在以下代码适当的AbstractFormatterFactory和AbstractFormatter将创建来处理格式化数字:
jformattedtextfield TF =新jformattedtextfield();TF。setValue(新号码(100));
警告:为AbstractFormatter通常会在Document安装DocumentFilter,和你不应该安装你自己的JFormattedTextField一NavigationFilter。如果你这样做,你可能会看到奇怪的行为在AbstractFormatter编辑方针将不会被执行。
警告: Swing是线程不安全的。更多信息见Swing's Threading Policy。
警告:序列化该类的对象与以后的Swing版本不兼容。当前的序列化支持适用于短期贮藏或RMI运行相同Swing版本的应用程序之间。为1.4,为所有JavaBeans™长期存储的支持已被添加到java.beans包。请看XMLEncoder。
| Modifier and Type | Class and Description |
|---|---|
static class |
JFormattedTextField.AbstractFormatter
AbstractFormatter实例所使用的
JFormattedTextField处理从一个对象到字符串的转换,以及从一个字符串对象。
|
static class |
JFormattedTextField.AbstractFormatterFactory
AbstractFormatterFactory实例所使用的
JFormattedTextField获得
AbstractFormatter反过来用于格式化值的实例。
|
JTextField.AccessibleJTextFieldJTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBindingJComponent.AccessibleJComponentContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy| Modifier and Type | Field and Description |
|---|---|
static int |
COMMIT
不断识别时失去了焦点,
commitEdit应该调用。
|
static int |
COMMIT_OR_REVERT
不断识别时失去了焦点,
commitEdit应该调用。
|
static int |
PERSIST
常量确定当焦点丢失时,编辑的值应该被留下。
|
static int |
REVERT
不断识别时失去了焦点,编辑值应恢复电流值设置在
JFormattedTextField。
|
notifyActionDEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEYlistenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTBOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WESTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
JFormattedTextField()
创建一个没有
AbstractFormatterFactory
JFormattedTextField。
|
JFormattedTextField(Format format)
创建一个
JFormattedTextField。
|
JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
创建一个具有指定
AbstractFormatter
JFormattedTextField。
|
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
创建一个具有指定
AbstractFormatterFactory
JFormattedTextField。
|
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
创建一个具有指定
AbstractFormatterFactory和初始值
JFormattedTextField。
|
JFormattedTextField(Object value)
创建一个具有指定值的jformattedtextfield。
|
| Modifier and Type | Method and Description |
|---|---|
void |
commitEdit()
目前的价值力量是从
AbstractFormatter和设置为当前值。
|
Action[] |
getActions()
获取编辑命令列表。
|
int |
getFocusLostBehavior()
返回焦点丢失时的行为。
|
JFormattedTextField.AbstractFormatter |
getFormatter()
返回
AbstractFormatter,用于格式化和解析当前值。
|
JFormattedTextField.AbstractFormatterFactory |
getFormatterFactory()
返回当前
AbstractFormatterFactory。
|
String |
getUIClassID()
获取用户界面的类标识。
|
Object |
getValue()
返回最后一个有效值。
|
protected void |
invalidEdit()
当用户输入一个无效值时调用。
|
boolean |
isEditValid()
如果当前正在编辑的值是有效的,则返回真。
|
protected void |
processFocusEvent(FocusEvent e)
处理任何焦点事件,如
FocusEvent.FOCUS_GAINED或
FocusEvent.FOCUS_LOST。
|
protected void |
processInputMethodEvent(InputMethodEvent e)
处理任何输入法事件,如
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED或
InputMethodEvent.CARET_POSITION_CHANGED。
|
void |
setDocument(Document doc)
将编辑器与文本文档关联。
|
void |
setFocusLostBehavior(int behavior)
设置焦点丢失时的行为。
|
protected void |
setFormatter(JFormattedTextField.AbstractFormatter format)
设置当前
AbstractFormatter。
|
void |
setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
集
AbstractFormatterFactory。
|
void |
setValue(Object value)
设置值将从目前的
AbstractFormatterFactory获得
AbstractFormatter格式化。
|
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, setScrollOffsetaddCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, writeaddAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateadd, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeaction, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, 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, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic static final int COMMIT
commitEdit应该调用。如果在提交新值
ParseException扔,无效的值将保持。
public static final int COMMIT_OR_REVERT
commitEdit应该调用。如果在提交新值
ParseException抛出,该值将恢复。
public static final int REVERT
JFormattedTextField。
public static final int PERSIST
public JFormattedTextField()
AbstractFormatterFactory
JFormattedTextField。使用
setMask或
setFormatterFactory配置
JFormattedTextField编辑一个特定类型的值。
public JFormattedTextField(Object value)
value类型
AbstractFormatterFactory。
value - JFormattedTextField初始值
public JFormattedTextField(Format format)
JFormattedTextField。
format是包裹在一个适当的
AbstractFormatter然后裹在一个
AbstractFormatterFactory。
format格式用于查找一个abstractformatter
public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
AbstractFormatter
JFormattedTextField。的
AbstractFormatter是放置在一个
AbstractFormatterFactory。
formatter - abstractformatter使用格式。
public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
AbstractFormatterFactory
JFormattedTextField。
factory -用于合成AbstractFormatterFactory。
public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
AbstractFormatterFactory和初始值
JFormattedTextField。
factory -用于格式化
AbstractFormatterFactory。
currentValue初始值使用
public void setFocusLostBehavior(int behavior)
JFormattedTextField.COMMIT_OR_REVERT,
JFormattedTextField.REVERT,
JFormattedTextField.COMMIT或
JFormattedTextField.PERSIST注意一些
AbstractFormatters可能促使他们发生的变化,使这种价值没有影响。
这会如果传递的对象不是一个上述值抛出IllegalArgumentException。
此属性的默认值是JFormattedTextField.COMMIT_OR_REVERT。
behavior识别行为时失去了焦点
IllegalArgumentException如果行为不是一个已知的值
public int getFocusLostBehavior()
COMMIT_OR_REVERT,
COMMIT,
REVERT或
PERSIST注意一些
AbstractFormatters可能促使他们发生的变化,使这种价值没有影响。
public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
AbstractFormatterFactory。
AbstractFormatterFactory能够返回
AbstractFormatter是用于格式显示值的实例,以及执行编辑政策。
如果你没有显式设置一个AbstractFormatterFactory通过这种方法(或构造函数)的AbstractFormatterFactory因此AbstractFormatter将采用基于价值的类。NumberFormatter将用于Numbers,DateFormatter将用于Dates DefaultFormatter,否则将使用。
这是一个JavaBeans属性绑定。
tf用于查找实例
AbstractFormatter
AbstractFormatterFactory
public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
AbstractFormatterFactory。
AbstractFormatterFactory用来确定
AbstractFormatters
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)
protected void setFormatter(JFormattedTextField.AbstractFormatter format)
AbstractFormatter。
你通常不应该调用这个,而不是设置AbstractFormatterFactory或设定值。JFormattedTextField将调用这JFormattedTextField状态变化和需要的值被重置。JFormattedTextField通过从AbstractFormatterFactory获得AbstractFormatter。
这是一个JavaBeans属性绑定。
format - abstractformatter使用格式
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)
public JFormattedTextField.AbstractFormatter getFormatter()
AbstractFormatter,用于格式化和解析当前值。
public void setValue(Object value)
AbstractFormatterFactory获得
AbstractFormatter格式化。如果没有
AbstractFormatterFactory已指定,这将创建一个基于
value类型。
此属性的默认值为空。
这是一个JavaBeans属性绑定。
value电流值显示
public Object getValue()
AbstractFormatter这可能不会返回当前值。当前编辑的价值可以通过调用
commitEdit随后
getValue获得。
public void commitEdit()
throws ParseException
AbstractFormatter和设置为当前值。这有没有影响,如果没有当前
AbstractFormatter安装。
ParseException -如果
AbstractFormatter无法格式的当前值
public boolean isEditValid()
AbstractFormatter管理,因此它没有公共的二传手。
protected void invalidEdit()
protected void processInputMethodEvent(InputMethodEvent e)
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED或
InputMethodEvent.CARET_POSITION_CHANGED。
processInputMethodEvent 方法重写,继承类
JTextComponent
e -
InputMethodEvent
InputMethodEvent
protected void processFocusEvent(FocusEvent e)
FocusEvent.FOCUS_GAINED或
FocusEvent.FOCUS_LOST。
processFocusEvent 方法重写,继承类
Component
e -
FocusEvent
FocusEvent
public Action[] getActions()
getActions 方法重写,继承类
JTextField
public String getUIClassID()
getUIClassID 方法重写,继承类
JTextField
JComponent.getUIClassID()
public void setDocument(Document doc)
setDocument 方法重写,继承类
JTextField
doc -文档显示/编辑
JTextComponent.getDocument()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.