How to show only date after the date of today in JCalendar

佐手、 提交于 2019-12-17 20:35:54

问题


i'm trying to limit user to select only the date after today, or select date after another Date I see on JCalendar API something that could help me but i didn't find nothing.. how can i do it?


回答1:


I do not think there is a straight forward way on the component to do this. One way, that I know of is to use the setSelectableDateRange(Date from,Date to) - When you set the from date to current date, all previous day cells, year/month drop downs becomes disabled.

Example:

    JCalendar calendar = new JCalendar();
    calendar.setSelectableDateRange(new Date(),new SimpleDateFormat("MM-DD-YYYY").parse("05-05-2015"));

    PropertyChangeListener calendarChangeListener  = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Date selectedDate = ((JCalendar)evt.getSource()).getDate();
        }
    };
    calendar.addPropertyChangeListener("calendar",calendarChangeListener);

This will disable selection of any date before current date and after 05/05/2015

Note that this API is not documented in their javadoc. But still this is a public setter that works as expected.

EDIT since you want to know how JDateChooser can be used in similar context

    JDateChooser chooser = new JDateChooser();
    chooser.getJCalendar().setSelectableDateRange(new Date(),new SimpleDateFormat("MM-DD-YYYY").parse("05-05-2015"));
    chooser.getJCalendar().addPropertyChangeListener("calendar",...);


来源:https://stackoverflow.com/questions/10021565/how-to-show-only-date-after-the-date-of-today-in-jcalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!