在阅读了这篇文章和这个问题之后,我尝试将两者结合起来:一个总是以正确的位置显示斜杠并自动解析Date对象的JFormattedTextField。
我想出的代码如下:
private DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
textField_DaRiassuntoIncassi = new JFormattedTextField(df);
textField_ARiassuntoIncassi = new JFormattedTextField(df);
textField_DaScadenze = new JFormattedTextField(df);
textField_AScadenze = new JFormattedTextField(df);
textField_DaRiassuntoIncassi.setColumns(10);
textField_ARiassuntoIncassi .setColumns(10);
textField_DaScadenze .setColumns(10);
textField_AScadenze .setColumns(10);
try
{
MaskFormatter dateMask = new MaskFormatter("##/##/####");
dateMask.install(textField_DaRiassuntoIncassi);
dateMask.install(textField_ARiassuntoIncassi);
dateMask.install(textField_DaScadenze);
dateMask.install(textField_AScadenze);
}
catch(ParseException ex)
{
ex.printStackTrace();
}问题是,当我单击textfield输入值时,当我键入两个斜杠时,我希望它们保持固定(就像按下键盘上的“插入”键时一样)。如果我将MaskFormatter放在构造函数中,那么问题就消失了,但是我可以在textfield中输入我想输入的任何数字,比如"99/00/9874“,组件告诉我这是一个ok值,因为我不知道在哪里插入SimpleDateFormat。
我的最后一招是将MaskFormatter放入JFormattedTextField构造函数中,使用getText()方法获取文本,尝试使用DateFormat解析日期,并在出错的情况下做一些事情,但我认为有一种聪明的方法。我试着用这种方法
textField_AScadenze.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(new SimpleDateFormat("dd/MM/yyyy"))));但一旦我不插入任何东西,点击出来,斜杠就消失了。请帮帮忙。谢谢
发布于 2013-05-05 20:25:42
。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;
public class TimeFormatter extends MaskFormatter {
private static final long serialVersionUID = 1L;
public TimeFormatter() { // set mask and placeholder
try {
setMask("##/##/####");
setPlaceholderCharacter('0');
setAllowsInvalid(false);
setOverwriteMode(true);
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
public Object stringToValue(String string) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
if (string == null) {
string = "00/00/0000";
}
return df.parse(string);
}
@Override
public String valueToString(Object value) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
if (value == null) {
value = new Date(0);
}
return df.format((Date) value);
}
private void MyGui() {
final MaskFormatter formatter = new TimeFormatter(); // textfield 1: create formatter and textfield
//formatter.setValueClass(java.util.Date.class);
final JFormattedTextField tf2 = new JFormattedTextField(formatter);// textfield 2: create formatter and textfield
tf2.setValue(new Date()); // no initial value
final JLabel label = new JLabel();
JButton bt = new JButton("Show Value");// button to show current value
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(" value 2 = " + tf2.getValue());
System.out.println(" value 2 = " + tf2.getText());
System.out.println("value class: " + formatter.getValueClass());
label.setText(tf2.getText());
}
});
JFrame f = new JFrame(); // main frame
f.getContentPane().setLayout(new GridLayout());
f.getContentPane().add(tf2);
f.getContentPane().add(label);
f.getContentPane().add(bt);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) throws Exception {
Runnable doRun = new Runnable() {
@Override
public void run() {
new TimeFormatter().MyGui();
}
};
SwingUtilities.invokeLater(doRun);
}
}https://stackoverflow.com/questions/16386661
复制相似问题