如何使JDatePicker文本字段格式化为输入?

时间:2016-02-08 07:51:07

标签: java swing datepicker simpledateformat jformattedtextfield

使用org.jdatepicker

在我的应用输入字段中应该是可编辑的,所以我添加了

    datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
    datePicker.setTextEditable(true);

但现在我可以写任何bs了: screenshot。 我需要添加像

这样的东西
    mf = new MaskFormatter("##.##.####"); 
    mf .setPlaceholderCharacter('.');

将输入限制为遮罩。但是怎么做呢? JDatePickerImpl不是JFormattedTextField。 当我从日历中选择日期时,它的格式正确。我的DateLabelFormatter类:

    public class DateLabelFormatter extends JFormattedTextField.AbstractFormatter {
        private String datePattern = "dd.MM.yyyy";
        private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
     public Object stringToValue(String text) throws ParseException {
     return dateFormatter.parseObject(text);
      }
    @Override
        public String valueToString(Object value) throws ParseException {
            if (value != null) {
                Calendar cal = (Calendar) value;
                return dateFormatter.format(cal.getTime());
            }
            return "";
        }
    }

加入: 试图从JDatePickerImpl中提取JFormattedTextField并改变这种方式:

     datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
     datePicker.setTextEditable(true);

     MaskFormatter mf = new MaskFormatter("##.##.####");
     mf.setValidCharacters("1234567890");
     mf.setPlaceholderCharacter(' ');
     JFormattedTextField textField = datePicker.getJFormattedTextField();
     textField.setFormatterFactory(new DefaultFormatterFactory(mf));

现在看起来like I wanted,但似乎其他一切都出了问题,现在还没有回复日期。

还尝试更改DateLabelFormatter类:

    public class DateLabelFormatter extends MaskFormatter {

private String datePattern = "dd.MM.yyyy";
private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
public DateLabelFormatter() { // set mask and placeholder
    try {
        setMask("##.##.####");

        setPlaceholderCharacter('0');
        setAllowsInvalid(false);
        setOverwriteMode(true);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
@Override
public Object stringToValue(String text) throws ParseException {
    return dateFormatter.parseObject(text);
}

@Override
public String valueToString(Object value) throws ParseException {
    if (value != null) {
        Calendar cal = (Calendar) value;
        return dateFormatter.format(cal.getTime());
    }

    return dateFormatter.format(Calendar.DATE);
}

}

当我从日历中选择日期时,现在一切看起来都很好甚至部分有效。但是当我从键盘输入日期时我得到错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.util.Calendar
at org.jdatepicker.impl.JDatePickerImpl$InternalEventHandler.propertyChange(JDatePickerImpl.java:247)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.Component.firePropertyChange(Component.java:8430)
at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:798)
at javax.swing.JFormattedTextField.commitEdit(JFormattedTextField.java:529)
at javax.swing.JFormattedTextField$FocusLostHandler.run(JFormattedTextField.java:648)
at javax.swing.JFormattedTextField.processFocusEvent(JFormattedTextField.java:630)
at java.awt.Component.processEvent(Component.java:6291)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:995)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
at java.awt.Component.dispatchEventImpl(Component.java:4762)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题:似乎stringToValue()必须返回Calendar的实例,而不是Date,对称地接收一个日历作为输入的valueToString()。所以正确的代码是:

public class DateLabelFormatter extends JFormattedTextField.AbstractFormatter {
    private String datePattern = "dd.MM.yyyy";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime((Date) dateFormatter.parseObject(text));
        return cal;
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if (value != null) {
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }
        return "";
    }
}