public class InternationalFormatter extends DefaultFormatter
InternationalFormatter延伸DefaultFormatter ,使用的实例java.text.Format来处理转换为字符串,并从字符串的转换。
如果getAllowsInvalid()为false,这将要求Format格式化每个编辑的当前文本。
您可以通过setMinimum和setMaximum方法指定最小值和最大值。 为了使这项工作中的值返回stringToValue必须与通过的方式,最小值/最大值Comparable接口。
请注意如何配置Format和InternationalFormatter ,因为可以创建无法输入某些值的情况。 考虑日期格式“M / D / Y-Y”,一个InternationalFormatter ,始终是有效的( setAllowsInvalid(false) ),是在覆盖模式( setOverwriteMode(true) )和日期99年7月1日。 在这种情况下,用户将无法输入两位数字的月份或月份。 为避免这种情况,格式应为“MM / dd / yy”。
如果InternationalFormatter被配置为只允许有效值( setAllowsInvalid(false) ),每一个有效的编辑将导致文本JFormattedTextField是从完全复位Format 。 光标位置也将被调整为文字字符从结果字符串中添加/删除。
InternationalFormatter的行为为stringToValue与stringToValue略有DefaultTextFormatter ,它执行以下操作:
parseObject被Format指定的setFormat setValueClass ),那么将调用parseObject实现来将从parseObject返回的值转换为适当的类。 ParseException没有抛出,并且该值超出了最小/最大值,则抛出一个ParseException 。 InternationalFormatter以这种方式实现stringToValue ,这样就可以指定一个替代Class,比Format可能返回。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,支持所有JavaBeans的长期存储已被添加到java.beans包中。 请参阅XMLEncoder 。
Format , Comparable
| Constructor and Description |
|---|
InternationalFormatter()
创建一个
InternationalFormatter没有
Format规定。
|
InternationalFormatter(Format format)
创建一个
InternationalFormatter与指定的
Format实例。
|
| Modifier and Type | Method and Description |
|---|---|
Object |
clone()
创建一个DefaultFormatter的副本。
|
protected Action[] |
getActions()
如果
getSupportsIncrement返回true,则返回两个适用于递增/递减值的动作。
|
Format.Field[] |
getFields(int offset)
返回与
Format.Field的文本相关联的
offset 。
|
Format |
getFormat()
返回指定可编辑和显示的合法值的格式。
|
Comparable |
getMaximum()
返回最大允许值。
|
Comparable |
getMinimum()
返回最小允许值。
|
void |
install(JFormattedTextField ftf)
将
DefaultFormatter安装到特定的
JFormattedTextField 。
|
void |
setFormat(Format format)
设置指定可编辑和显示的合法值的格式。
|
void |
setMaximum(Comparable max)
设置最大允许值。
|
void |
setMinimum(Comparable minimum)
设置最小允许值。
|
Object |
stringToValue(String text)
返回
Object的
String
text 。
|
String |
valueToString(Object value)
返回Object
value的String表示
value 。
|
getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClassgetFormattedTextField, invalidEdit, setEditValid, uninstallpublic InternationalFormatter()
InternationalFormatter没有
Format规定。
public InternationalFormatter(Format format)
InternationalFormatter与指定的
Format实例。
format - 用于从/转换到字符串的格式化实例
public void setFormat(Format format)
format -
Format用于从/转换到字符串的实例
public Format getFormat()
public void setMinimum(Comparable minimum)
valueClass尚未指定,并minimum为非null,则valueClass将被设置为该类的minimum 。
minimum - 可输入的最低法定价值
DefaultFormatter.setValueClass(java.lang.Class<?>)
public Comparable getMinimum()
public void setMaximum(Comparable max)
valueClass尚未指定,并max为非null,则valueClass将被设置为该类的max 。
max - 可以输入的最大合法值
DefaultFormatter.setValueClass(java.lang.Class<?>)
public Comparable getMaximum()
public void install(JFormattedTextField ftf)
DefaultFormatter安装到特定的JFormattedTextField 。
这将调用valueToString将当前值从JFormattedTextField转换为字符串。
那么这将安装Action从s getActions ,该DocumentFilter从返回getDocumentFilter和NavigationFilter从返回getNavigationFilter到JFormattedTextField 。
子类通常只需要覆盖这个,如果他们希望在JFormattedTextField上安装其他监听JFormattedTextField 。
如果有一个ParseException当前值转换为字符串,这将设置文本为空字符串,并标记JFormattedTextField为无效状态。
虽然这是一种公共方法,但这通常仅适用于JFormattedTextField的JFormattedTextField 。 JFormattedTextField将在值更改或其内部状态更改的适当时候调用此方法。
install在
DefaultFormatter
ftf - JFormattedTextField格式化,可能为空,表示从当前JFormattedTextField卸载。
public String valueToString(Object value) throws ParseException
value的String表示value 。
这将调用format对当前Format 。
valueToString在类
DefaultFormatter
value - 要转换的值
ParseException - 如果转换中有错误
public Object stringToValue(String text) throws ParseException
Object text的
String
text 。
stringToValue在
DefaultFormatter
text -
String转换
Object表示文字
ParseException - 如果转换中有错误
public Format.Field[] getFields(int offset)
Format.Field的文本相关联的offset 。
如果offset不是当前文本中的有效位置,这将返回一个空数组。
offset - 偏移到要检查的文本
public Object clone() throws CloneNotSupportedException
clone在
DefaultFormatter
CloneNotSupportedException - 如果对象的类不支持Cloneable接口。
覆盖clone方法的子类也可以抛出此异常以指示实例无法克隆。
Cloneable
protected Action[] getActions()
getSupportsIncrement返回true,则返回两个适用于递增/递减值的动作。
getActions在
JFormattedTextField.AbstractFormatter类
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.