java - How to make JDatePicker text field formatted for input? -
org.jdatepicker used
in app input field should editable, added
datepicker = new jdatepickerimpl(datepanel, new datelabelformatter()); datepicker.settexteditable(true);
but can write bs it: screenshot. need add
mf = new maskformatter("##.##.####"); mf .setplaceholdercharacter('.');
to limit input mask. how this? jdatepickerimpl isn't jformattedtextfield. when choose date calendar it's formatted right way. datelabelformatter class:
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 ""; } }
added: tried extract jformattedtextfield jdatepickerimpl , changed way:
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));
it looks like wanted now, seems else went wrong , it's not returning date now.
also tried change datelabelformatter class:
public class datelabelformatter extends maskformatter { private string datepattern = "dd.mm.yyyy"; private simpledateformat dateformatter = new simpledateformat(datepattern); public datelabelformatter() { // set mask , 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); }
}
everything looks fine , partially works - when pick date calendar. when type date keyboard error:
exception in thread "awt-eventqueue-0" java.lang.classcastexception: java.util.date cannot cast java.util.calendar @ org.jdatepicker.impl.jdatepickerimpl$internaleventhandler.propertychange(jdatepickerimpl.java:247) @ java.beans.propertychangesupport.fire(propertychangesupport.java:335) @ java.beans.propertychangesupport.firepropertychange(propertychangesupport.java:328) @ java.beans.propertychangesupport.firepropertychange(propertychangesupport.java:263) @ java.awt.component.firepropertychange(component.java:8430) @ javax.swing.jformattedtextfield.setvalue(jformattedtextfield.java:798) @ javax.swing.jformattedtextfield.commitedit(jformattedtextfield.java:529) @ javax.swing.jformattedtextfield$focuslosthandler.run(jformattedtextfield.java:648) @ javax.swing.jformattedtextfield.processfocusevent(jformattedtextfield.java:630) @ java.awt.component.processevent(component.java:6291) @ java.awt.container.processevent(container.java:2236) @ java.awt.component.dispatcheventimpl(component.java:4891) @ java.awt.container.dispatcheventimpl(container.java:2294) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.keyboardfocusmanager.redispatchevent(keyboardfocusmanager.java:1954) @ java.awt.defaultkeyboardfocusmanager.typeaheadassertions(defaultkeyboardfocusmanager.java:995) @ java.awt.defaultkeyboardfocusmanager.dispatchevent(defaultkeyboardfocusmanager.java:661) @ java.awt.component.dispatcheventimpl(component.java:4762) @ java.awt.container.dispatcheventimpl(container.java:2294) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:758) @ java.awt.eventqueue.access$500(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:709) @ java.awt.eventqueue$3.run(eventqueue.java:703) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:731) @ java.awt.eventqueue$4.run(eventqueue.java:729) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:728) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82)
i had same problem: seems stringtovalue()
has return instance of calendar , not of date, symmetrically valuetostring()
receives calendar input. correct code be:
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 ""; } }
Comments
Post a Comment