Disable multiple date ranges jDateChooser

前端 未结 2 728
梦如初夏
梦如初夏 2020-12-04 03:42

I want to disable multiple date ranges on a JCalendar. I\'m following these steps, but I need to know how can I add multiple date evaluators. Help me please, thanks.

相关标签:
2条回答
  • 2020-12-04 04:03

    Just call addDateEvaluator() for each RangeEvaluator that you create. This adds the RangeEvaluator to a List dateEvaluators inside JDayChooser. Later, JDayChooser iterates over the list when it decides how to draw the day buttons.

    Edit: Here's the RangeEvaluator I used.

    private static class RangeEvaluator extends MinMaxDateEvaluator {
    
        @Override
        public boolean isInvalid(Date date) {
            return !super.isInvalid(date);
        }
    }
    

    And here's how I used it.

    RangeEvaluator re = new RangeEvaluator();
    re.setMinSelectableDate(...);
    re.setMaxSelectableDate(...);
    JCalendar jc = new JCalendar();
    jc.getDayChooser().addDateEvaluator(re);
    

    One problem I noticed is that you have to tell the JDayChooser to reconfigure its buttons using the new evaluator. You can fire a property change event or just change a bound property.

    jc.setCalendar(jc.getCalendar());
    
    0 讨论(0)
  • 2020-12-04 04:24

    Based on your update there are two things that happen here.

    First is a little mistake when you instatiate your SimpleDateFormat:

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
    

    In this pattern "mm" refers to minutes not months. It should be:

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    

    Second problem is IMHO a bug since day chooser seems to not apply filters until a new date is set or it has to be repainted because of month/year change. You can test it changing month to August and then back to September: dates from 9/11 to 9/15 will be disabled. To solve this inconvenient behavior just set current date explicitely like this:

    JCalendar calendar = new JCalendar();
    calendar.getDayChooser().addDateEvaluator(evaluator);
    calendar.setDate(Calendar.getInstance().getTime());
    

    Side note: while this library is very very useful it has a couple of bug/design issues so don't hesitate to ask for help.

    0 讨论(0)
提交回复
热议问题