我想要一个文本字段,使用户能够输入特定模式之后的日期,并且我希望在输入和有效后立即返回此日期,即无需等待用户在文本字段上按返回处理它。此外,我还添加了视觉提示,无论输入的日期是否正确(更改文本字段的背景颜色)。
这是一个SSC不正确的例子
package gui.date;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class DateEditor extends JFormattedTextField
{
public DateEditor()
{
this(null, null);
}
public DateEditor(Date d)
{
this(d, null);
}
public DateEditor(Date d, String pattern)
{
super(new SimpleDateFormat(pattern));
setValue(d);
setFocusLostBehavior(JFormattedTextField.PERSIST);
final Color origBgdColor = getBackground();
final Color wrongInputColor = new Color(0.8f, .0f, .0f);
final Color missingInputColor = new Color(1f, 1f, .65f);
if(d == null)
{
setColumns(10);
setBackground(missingInputColor);
}
addCaretListener(new CaretListener()
{
@Override
public void caretUpdate(CaretEvent e)
{
if(isEditValid())
{
try
{
setBackground(origBgdColor);
commitEdit();
}
catch(ParseException ex)
{
ex.printStackTrace();
}
}
else
{
try
{
String text = getText();
if(text.isEmpty())
setBackground(missingInputColor);
else
setBackground(wrongInputColor);
}
catch(NullPointerException ex)
{
setBackground(missingInputColor);
}
}
}
});
}
public Date getDate()
{
return (Date)getValue();
}
public void setDate(Date d)
{
setValue(d);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
final Date[] date = new Date[] { null }; // the date entered by the user should be filled here as soon as it is a valid date
final String pattern = "dd/MM/YYYY";
final SimpleDateFormat fm = new SimpleDateFormat(pattern);
JButton b = new JButton(new AbstractAction("Print value")
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
System.out.println(fm.format(date[0]));
}
catch(NullPointerException ex)
{
System.out.println("No date set yet");
}
}
});
final DateEditor d = new DateEditor(null, pattern);
d.addPropertyChangeListener("value", new PropertyChangeListener()
{
@Override
public void propertyChange(PropertyChangeEvent evt)
{
if(evt.getSource().equals(d))
{
date[0] = d.getDate();
}
}
});
final JFrame f = new JFrame(getClass().getName());
Container c = f.getContentPane();
c.setLayout(new FlowLayout());
c.add(b);
c.add(d);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}
如果您尝试一下,您会看到几个错误: 1 /我指定的模式似乎不是由FormattedText应用的,这样当我按下回车键时输入的有效日期可以转换为另一个日期
尽管我首先检查isEditValid()
!,但在CaretListener中调用commitEdit时仍会引发 2 / ParseException。
谢谢你的帮助。