public class JFormattedTextField extends JTextField
JFormattedTextField
延伸
JTextField
添加格式任意值的支持,以及检索一个特定的对象,一旦用户编辑文本。下面的说明配置
JFormattedTextField
编辑日期:
jformattedtextfield FTF =新jformattedtextfield();FTF setValue(新());
一旦JFormattedTextField
已经创建,你可以聆听编辑修改通过添加一个PropertyChangeListener
倾听与属性名称value
PropertyChangeEvent
s。
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.AbstractFormatter
s将每个编辑可使新的价值观,和其他人将不会承诺的价值。你可以强制电流值是通过调用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.AccessibleJTextField
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.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 。
|
notifyAction
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
ABORT, 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, setScrollOffset
addCaretListener, 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, write
addAncestorListener, 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, update
add, 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, validateTree
action, 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, transferFocusUpCycle
public 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
注意一些
AbstractFormatter
s可能促使他们发生的变化,使这种价值没有影响。
这会如果传递的对象不是一个上述值抛出IllegalArgumentException
。
此属性的默认值是JFormattedTextField.COMMIT_OR_REVERT
。
behavior
识别行为时失去了焦点
IllegalArgumentException
如果行为不是一个已知的值
public int getFocusLostBehavior()
COMMIT_OR_REVERT
,
COMMIT
,
REVERT
或
PERSIST
注意一些
AbstractFormatter
s可能促使他们发生的变化,使这种价值没有影响。
public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
AbstractFormatterFactory
。
AbstractFormatterFactory
能够返回
AbstractFormatter
是用于格式显示值的实例,以及执行编辑政策。
如果你没有显式设置一个AbstractFormatterFactory
通过这种方法(或构造函数)的AbstractFormatterFactory
因此AbstractFormatter
将采用基于价值的类
。NumberFormatter
将用于Number
s,DateFormatter
将用于Dates
DefaultFormatter
,否则将使用。
这是一个JavaBeans属性绑定。
tf
用于查找实例
AbstractFormatter
AbstractFormatterFactory
public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
AbstractFormatterFactory
。
AbstractFormatterFactory
用来确定
AbstractFormatter
s
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.