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

前端 未结 1 667
清歌不尽
清歌不尽 2020-12-10 22:49

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\

相关标签:
1条回答
  • 2020-12-10 23:37

    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",...);
    
    0 讨论(0)
提交回复
热议问题