[英]How to use jXDatePicker with maskFormatter?
I would like to use a jxdatepicker with maskFormatter. 我想使用带有maskFormatter的jxdatepicker。 I tried 我试过了
MaskFormatter maskFormatter = new MaskFormatter ("##/##/####");
JFormattedTextField field=new JFormattedTextField (maskFormatter);
jXDatePicker.setEditor (field);
and 和
MaskFormatter maskFormatter = new MaskFormatter ("##/##/####");
maskFormatter.install (jXDatePicker.getEditor ());
neither the first nor the second solution worked 第一种和第二种解决方案都不起作用
PS: A JFormattedTextField
work fine with MaskFormatter
AND jXDatePicker
work fine with a simple JFormattedTextField
PS:一个JFormattedTextField
可以正常使用MaskFormatter
和jXDatePicker
与一个简单的JFormattedTextField
jXDatePicker
正常工作
This is an old question, but seems to be still active, so here is how we implemented the functionality some time ago ( swingx-all-1.6.5-1.jar
): 这是一个老问题,但似乎仍然是活跃的,所以这是我们在不久前实现功能的方式( swingx-all-1.6.5-1.jar
):
1) Create a wrapper class for MaskFormatter
1)为 MaskFormatter
创建一个包装类
public class Wrapper extends MaskFormatter {
private final static String DD_MM_YYY = "dd/MM/yyyy";
public Wrapper(String string) throws ParseException {
super(string);
}
@Override
public Object stringToValue(String value) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(DD_MM_YYY);
Date parsed = format.parse(value);
return parsed;
}
public String valueToString(Object value) throws ParseException {
if (value != null) {
SimpleDateFormat format = new SimpleDateFormat(DD_MM_YYY);
String formated = format.format((Date) value);
return super.valueToString(formated);
} else {
return super.valueToString(value);
}
}
}
2) Add the wrapped Formatter to the JFormattedTextField
and set it on the JXDatePicker
2)将包装的Formatter添加到 JFormattedTextField
并在 JXDatePicker
上设置它
MaskFormatter maskFormatter;
JXDatePicker datePicker = new JXDatePicker();
try {
maskFormatter = new Wrapper("##/##/####");
JFormattedTextField field = new JFormattedTextField(maskFormatter);
datePicker.setEditor(field);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
somePanel.add(datePicker);
The wrapper class basically does the formatting, since trying to set a DateFormat
on the JXDatePicker
led to various ParseException
. 包装类基本上执行格式化,因为尝试在JXDatePicker
上设置DateFormat
JXDatePicker
导致各种ParseException
。
Personally I'm not very skilled in Java but after checking some docs quickly. 就个人而言,我不是很熟悉Java,但很快就检查了一些文档。 I think setEditor
is not the way to go. 我认为setEditor
不是要走的路。 With maskFormatter.install
you seem to go into the right direction. 使用maskFormatter.install
您似乎正朝着正确的方向前进。 Something like this might help you out: 这样的事情可能会帮助你:
JXDatePicker picker = new JXDatePicker();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
picker.setFormats(format);
Selective source: JXDatePicker using SimpleDateFormat to format dd.MM.yy to dd.MM.yyyy with current century 选择性来源: JXDatePicker使用SimpleDateFormat将dd.MM.yy格式化为当前世纪的dd.MM.yyyy
Or check out this: https://stackoverflow.com/a/9036979/4820655 或者看看这个: https : //stackoverflow.com/a/9036979/4820655
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.