【问题标题】:How to disable or highlight the dates in JCalendar如何禁用或突出显示 JCalendar 中的日期
【发布时间】:2013-09-16 04:05:58
【问题描述】:

就我而言,我想禁用或突出显示 Java 日历中的日期。我使用了JCalendarDateChooserCombo,但找不到方法。最后,我尝试了下面的代码,它也没有成功。

例如:我想禁用从14-09-1323-09-13 的所有日期。

DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
try {
     Date d1 = formatter.parse("2013-09-14");
     Date d2 = formatter.parse("2013-09-23");
     jCalendar1.setSelectableDateRange(d1, d2);
 } catch (ParseException ex) {
    ex.printStackTrace();
 }

【问题讨论】:

    标签: java swing simpledateformat jcalendar jdatechooser


    【解决方案1】:

    我知道这已经有一段时间了,但希望它对某人有用。这里的关键是实现IDateEvaluator 接口,该接口旨在验证日期是否特殊或无效。不幸的是,JCalendar 库只提供了一个具体的实现,即MinMaxDateEvaluatorclass,但以此为起点并不是那么复杂。

    范围评估器

    这里是一个实现的例子,请特别注意isInvalid(Date date)方法。此外,您可能还想查看 DateUtil 类,它也是 JCalendar 库的一部分。

    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();
        }        
    }
    

    使用RangeEvaluatorclass

    下面有一个使用RangeEvaluator 类的例子。请注意,9 月 14 日至 9 月 23 日的范围已禁用。

    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
    

    截图

    【讨论】:

    • 感谢您的回复。我会检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多