How to disable or highlight the dates in JCalendar

前端 未结 1 976
深忆病人
深忆病人 2020-12-03 16:28

In my case i want to disable or highlight dates in Java calendar. I used JCalendar and DateChooserCombo and could not find a way to do it. Finally,

相关标签:
1条回答
  • 2020-12-03 17:11

    I know this has been inactive for a while but hopefully it can be useful to someone. The key here is implement IDateEvaluator interface which is intended to validate if a date is special or invalid. Unfortunately there's only one concrete implementation provided with JCalendar library which is MinMaxDateEvaluatorclass, but taking this as start point is not so complicated.

    RangeEvaluator

    Here is an example of implementation, please pay special attention to isInvalid(Date date) method. Also you may want to look at DateUtil class which is also part of JCalendar library.

    class RangeEvaluator implements IDateEvaluator {
    
        private DateUtil dateUtil = new DateUtil();
    
        @Override
        public boolean isSpecial(Date date) {
            return false;
        }
    
        @Override
        public Color getSpecialForegroundColor() {
            return null;
        }
    
        @Override
        public Color getSpecialBackroundColor() {
            return null;
        }
    
        @Override
        public String getSpecialTooltip() {
            return null;
        }
        @Override
        public boolean isInvalid(Date date) {
            return dateUtil.checkDate(date);
            // if the given date is in the range then is invalid
        }        
    
        /**
         * Sets the initial date in the range to be validated.
         * @param startDate 
         */
        public void setStartDate(Date startDate) {
            dateUtil.setMinSelectableDate(startDate);
        }
    
        /**
         * @return the initial date in the range to be validated.
         */
        public Date getStartDate() {
            return dateUtil.getMinSelectableDate();
        }
    
        /**
         * Sets the final date in the range to be validated.
         * @param endDate 
         */
        public void setEndDate(Date endDate) {
            dateUtil.setMaxSelectableDate(endDate);
        }
    
        /**
         * @return the final date in the range to be validated.
         */
        public Date getEndDate() {
            return dateUtil.getMaxSelectableDate();
        }        
    }
    

    Using RangeEvaluatorclass

    There is an example of using RangeEvaluator class below. Please note the range from September 14th to September 23rd is disabled.

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    RangeEvaluator evaluator = new RangeEvaluator();
    evaluator.setStartDate(dateFormat.parse("2013-09-14"));
    evaluator.setEndDate(dateFormat.parse("2013-09-23"));
    
    JCalendar calendar = new JCalendar(Locale.US);
    calendar.getDayChooser().addDateEvaluator(evaluator); // evaluator must be added to a JDayChooser object
    

    Screenshot

    enter image description here

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