JavaTM 2 Platform
Standard Ed. 6

javax.swing
类 JFormattedTextField.AbstractFormatter

java.lang.Object
  继承者 javax.swing.JFormattedTextField.AbstractFormatter
所有已实现的接口:
Serializable
直接已知子类:
DefaultFormatter
正在封闭类:
JFormattedTextField

public abstract static class JFormattedTextField.AbstractFormatter
extends Object
implements Serializable

JFormattedTextField 所使用的 AbstractFormatter 实例,用于处理从 Object 到 String 和从 String 到 Object 的转换。AbstractFormatter 还可强制执行编辑策略,或者指导策略的执行,或者以任何适合于强制执行所需策略的方式操作 JFormattedTextField

AbstractFormatter 一次只能在一个 JFormattedTextField 中处于激活状态。JFormattedTextField 准备好使用 install 时即调用它,完成后调用 uninstall。希望安装其他状态的子类应该重写 install 并适当地通知超类。

子类必须重写转换方法 stringToValuevalueToString。或者,子类可以重写 getActionsgetNavigationFiltergetDocumentFilter,以便以特定的方式限制 JFormattedTextField

允许 JFormattedTextField 处于临时无效状态的子类应该在恰当的时间调用 setEditValid

从以下版本开始:
1.4

构造方法摘要
JFormattedTextField.AbstractFormatter()
           
 
方法摘要
protected  Object clone()
          复制 AbstractFormatter
protected  Action[] getActions()
          如果想提供自定义的 Action 集合,则应进行子类化和重写。
protected  DocumentFilter getDocumentFilter()
          如果想要提供某个 DocumentFilter 以限制输入内容,则应进行子类化和重写。
protected  JFormattedTextField getFormattedTextField()
          返回 AbstractFormatter 所安装到的当前 JFormattedTextField
protected  NavigationFilter getNavigationFilter()
          如果想要提供过滤器以限制用户可导航的位置,则应进行子类化和重写。
 void install(JFormattedTextField ftf)
          将 AbstractFormatter 安装到特定的 JFormattedTextField
protected  void invalidEdit()
          用户键入无效字符时调用此方法。
protected  void setEditValid(boolean valid)
          调用此方法以更新 JFormattedTextFieldeditValid 属性。
abstract  Object stringToValue(String text)
          分析 text,返回任意 Object。
 void uninstall()
          卸载 AbstractFormatter 可能已在 JFormattedTextField 上安装的所有状态。
abstract  String valueToString(Object value)
          返回为 value 显示的 string 值。
 
从类 java.lang.Object 继承的方法
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

JFormattedTextField.AbstractFormatter

public JFormattedTextField.AbstractFormatter()
方法详细信息

install

public void install(JFormattedTextField ftf)
AbstractFormatter 安装到特定的 JFormattedTextField。这会调用 valueToStringJFormattedTextField 的当前值转换为 String。然后将从 getActions 返回的 Action、从 getDocumentFilter 返回的 DocumentFilter 和从 getNavigationFilter 返回的 NavigationFilter 安装到 JFormattedTextField

如果子类希望在该 JFormattedTextField 上安装其他侦听器,通常只需重写此方法即可。

如果将当前值转换为 String 的过程中出现 ParseException,则会将文本设置为空 String,并标记 JFormattedTextField 为处于无效状态。

不过这是一个公共方法,通常它只对 JFormattedTextField 的子类有用。当值更改,或者 JFormattedTextField 的内部状态更改时,JFormattedTextField 才在适当的时间调用此方法。如果要创建 JFormattedTextField 的子类并且在不同于 JFormattedTextField 操作的时间安装/卸载 AbstractFormatter,则只需自己调用此方法。

参数:
ftf - 用于格式化的 JFormattedTextField,可以为 null,指示从当前 JFormattedTextField 进行卸载。

uninstall

public void uninstall()
卸载 AbstractFormatter 可能已在 JFormattedTextField 上安装的所有状态。这会重置在 JFormattedTextField 上安装的 DocumentFilterNavigationFilter 和其他 Action


stringToValue

public abstract Object stringToValue(String text)
                              throws ParseException
分析 text,返回任意 Object。某些 formatter 可能返回 null。

参数:
text - 要转换的 String
返回:
文本的 Object 表示形式
抛出:
ParseException - 如果转换过程中出现错误

valueToString

public abstract String valueToString(Object value)
                              throws ParseException
返回为 value 显示的 string 值。

参数:
value - 要转换的 Value
返回:
值的 String 表示形式
抛出:
ParseException - 如果转换过程中出现错误

getFormattedTextField

protected JFormattedTextField getFormattedTextField()
返回 AbstractFormatter 所安装到的当前 JFormattedTextField

返回:
用于格式化的 JFormattedTextField。

invalidEdit

protected void invalidEdit()
用户键入无效字符时调用此方法。这会将调用转发到当前的 JFormattedTextField。


setEditValid

protected void setEditValid(boolean valid)
调用此方法以更新 JFormattedTextFieldeditValid 属性。如果强制实施某个策略而使 JFormattedTextField 始终处于有效状态,则不再需要调用此方法。

参数:
valid - JFormattedTextField 的有效状态

getActions

protected Action[] getActions()
如果想提供自定义的 Action 集合,则应进行子类化和重写。install 将在 JFormattedTextFieldActionMap 上安装这些 Action。

返回:
要在 JFormattedTextField 上安装的 Action 数组

getDocumentFilter

protected DocumentFilter getDocumentFilter()
如果想要提供某个 DocumentFilter 以限制输入内容,则应进行子类化和重写。install 会将返回值安装到 JFormattedTextField 上。

返回:
限制编辑的 DocumentFilter

getNavigationFilter

protected NavigationFilter getNavigationFilter()
如果想要提供过滤器以限制用户可导航的位置,则应进行子类化和重写。install 会将返回值安装到 JFormattedTextField 上。

返回:
限制导航的 NavigationFilter

clone

protected Object clone()
                throws CloneNotSupportedException
复制 AbstractFormatter。返回的实例与 JFormattedTextField 无关联。

覆盖:
Object 中的 clone
返回:
AbstractFormatter 的副本
抛出:
CloneNotSupportedException - 如果对象的类不支持 Cloneable 接口,则重写 clone 方法的子类也会抛出此异常,以指示无法复制某个实例。
另请参见:
Cloneable

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策