JFormattedTextField组件和格式化输入

学习用最少的工作量达到格式化文本输入目的
作者:John Zukowski  翻译:Sean
2002.6.

数据挖掘实验室

利用输入验证和焦点监听接收格式化输入不会很困难。在这篇文章里,John向大家展示了如何用JFormattedTextField 组件提示输入数字,日期以及格式化的输入信息。 数据挖掘实验室

J2SE 1.4版本里面,加入了两个新的Swing组件:JSpinner和JFormattedTextField。我们在Merlin的魔术专栏的开始已经介绍了JSpinner组件,现在我们来了解一下JFormattedTextField。

数据挖掘工具

尽管JFormattedTextField组件的外表跟JTextField看上去是一样的,但它们的行为却是完全不同。最简单的情况下,你可以给出一个电话号码的输入掩码"(###) ###-####",JFormattedTextField不会接受任何不符合这样格式的输入。复杂点的情况是有一个显示格式和一个输入格式。举个例子来说,默认的日期输入格式允许有效的月份或者日期编辑情况下在光标的位置滚动。

数据挖掘论坛

JFormattedTextField可以接收的输入一是被mask显式定义好了,二则是被组件的某一个值所指定了。基于后一种情况下,组件用了Factory设计模式来为这个指定的值找到默认的formatter。DefaultFormatterFactory组件产生提前内置了的日期格式,数字格式,java.text.Format的子类,以及任何包罗万象的格式化器。

数据挖掘实验室

下面让我们来看看如何让这个组件工作起来。 数据挖掘实验室

配置可接收的输入
最经常地,我们用一个MaskFormatter实例来实现掩码输入。MaskFormatter类在javax.swing.text包中,用一系列字母来指定可输入的范围。这一系列八个字符每一个都代表了输入的一个字符,正如下面的列表:

# 一个数字                                                                                 
? 一个字母
A 一个数字或者字母
* 任何字符
U 一个字母,小写字母被转换到相应的大写字母
L 一个字母,大写字母被转换到相应的小子字母
H 一个十六进制数字(A-F, a-f, 0-9)
" 转义字符

数据挖掘研究院

除了MaskFormatter以外,你也可以使用DateFormat和NumberFormat类来指定输入格式。下表显示了一些可能的格式。

// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format =
  new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
// US Social Security number
MaskFormatter mf1 =
  new MaskFormatter("###-##-####");
// US telephone number
MaskFormatter mf2 =
  new MaskFormatter("(###) ###-####");


一旦你确定了输入格式,就可以传递formatter到JFormattedTextField类的构造器,如下示例:
JFormattedTextField ftf1 = new JFormattedTextField(df);
除了依赖于所使用的formatter,还有其他一些配置可选。例如,使用了MaskFormatter,你可以用setPlaceholderCharacter(char)方法设置placeholder字符。同样,日期输入框的话,它可以为你输入框初始化一个值,使得用户知道输入的格式。

总结
所有一切只是创建一个掩码的输入框。下面程序为你给出了一个完全的例子,该例子通过结合前面的代码片断检验了这个新的格式化输入能力。下面的图片则显示了外观。任意调整formatter来检测一下其他输入掩码。 数据挖掘研究院
代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.text.*;

public class FormattedSample {
  public static void main (String args[]) throws ParseException {
    JFrame f = new JFrame("JFormattedTextField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
    // Four-digit year, followed by month name and day of month,
    // each separated by two dashes (--)
    DateFormat format =
      new SimpleDateFormat("yyyy--MMMM--dd");
    DateFormatter df = new DateFormatter(format);
    JFormattedTextField ftf1 = new
      JFormattedTextField(df);
    ftf1.setValue(new Date());

数据挖掘交友


    content.add(ftf1);
    // US Social Security number
    MaskFormatter mf1 =
      new MaskFormatter("###-##-####");
    mf1.setPlaceholderCharacter("_");
    JFormattedTextField ftf2 = new
      JFormattedTextField(mf1);
    content.add(ftf2);
    // US telephone number
    MaskFormatter mf2 =
      new MaskFormatter("(###) ###-####");
    JFormattedTextField ftf3 = new
      JFormattedTextField(mf2);
    content.add(ftf3);
    f.setSize(300, 100);
    f.show();
  }
}

数据挖掘实验室

[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:NakedObjects框架: 最快最省构建漂亮合理的Java程序
下一篇:Google 开放式 WebServices 查询接口
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • 尚学堂J2EE和MLDN的J2EE视频教程哦
  • 有谁知道ERROR:JDWP Unable to get
  • com.microsoft.sqlserver.jdbc.SQLServerEx
  • 急~Eclipse3.3语言包,VEP插件
  • 毕业5年了,大家一个月全部收入能拿多少(税
  • org.hibernate.exception.ConstraintViolat
  • hibernate抛出could not fetch initial val
  • hibernate left outer join 出错 Path expe
  • myeclipse5.1.0和myeclipse6.0有什么区别。
  • spring如何动态获取bean,如何动态调用getB
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • 不安装jre能运行JAVA程序吗?
  • Java的模板引擎Velocity用户手册
  • 成为java高手的八大条件
  • 简化Java代码的技巧
  • JAVA的网络功能与编程
  • 用java读取各种计算机文件系统的文件列表
  • 怎样拿下SUN公司的SCJP认证?
  • Borland以全额现金交易收购VMGEAR
  • 用JNI技术提高Java的性能
  • 用Java编写HTML文件分析程序
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静