Java在JCalendar单元格中突出显示特定日期 [英] Java highlighting specific dates in JCalendar cell

查看:77
本文介绍了Java在JCalendar单元格中突出显示特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此处的代码设置了Toedter Calendar中的特定日期.我现在面临的问题是它没有突出显示正确的单元格.在我的示例中,我使用的是6月14日和15日,但是突出显示了8月9日和9日.

I followed the codes here to set the colors of a specific date in Toedter's Calendar. The problem I am facing now is that it is not highlighting the correct cell. In my example I have used 14th and 15th of June but it highlighted 8th and 9th.

这是我的代码:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    Date sdate= null;
    String d = null;
    for(int i =0;i<pd.size();i++){
        d = pd.get(i).getDate();
        try{

            sdate = (Date)formatter.parse(d); 
            if(events.contains(sdate)){

            }
            else{
                events.add(sdate);
                System.out.println(sdate);
            }

        }catch(ParseException r){
            System.out.println("error");
        }

    }

    //arraylist of events
    for(int i = 0; i < events.size(); i++)
    {   
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(events.get(i));
        int day1 = cal1.get(Calendar.DAY_OF_MONTH);
        int month1 = cal1.get(Calendar.MONTH);
        int year1 = cal1.get(Calendar.YEAR);
        //selected month and year on JCalendar
        if(month == month1 && year == year1)
        {
             // Calculate the offset of the first day of the month
             cal.set(Calendar.DAY_OF_MONTH,1);
             int offset = cal.get(Calendar.DAY_OF_WEEK) -1;
             component[day1 + offset ].setBackground(Color.blue); 
        }
    }

推荐答案

作为更改组件的替代方法,实现IDateEvaluator并返回所需的颜色,如建议的 List::contains 来标识特殊日期.只要确保清除您add()的日历日期上的时间字段即可.

As an alternative to changing the components, implement IDateEvaluator and return the desired colors, as suggested here. It's not clear where your Calendar offset goes awry. The example below uses List::contains to identify special dates. Just be sure to clear the time fields on the calendar dates you add().

import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JCalendar;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;

/**
 * @see https://stackoverflow.com/a/37899883/230513
 * @see https://stackoverflow.com/q/25501373/230513
 */
public class HighlightTest {

    private static class HighlightEvaluator implements IDateEvaluator {

        private final List<Date> list = new ArrayList<>();

        public void add(Date date) {
            list.add(date);
        }

        @Override
        public boolean isSpecial(Date date) {
            return list.contains(date);
        }

        @Override
        public Color getSpecialForegroundColor() {
            return Color.red.darker();
        }

        @Override
        public Color getSpecialBackroundColor() {
            return Color.blue;
        }

        @Override
        public String getSpecialTooltip() {
            return "Highlighted event.";
        }

        @Override
        public boolean isInvalid(Date date) {
            return false;
        }

        @Override
        public Color getInvalidForegroundColor() {
            return null;
        }

        @Override
        public Color getInvalidBackroundColor() {
            return null;
        }

        @Override
        public String getInvalidTooltip() {
            return null;
        }
    }

    private void display() {
        JFrame f = new JFrame("Highlight Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        HighlightEvaluator evaluator = new HighlightEvaluator();
        evaluator.add(createDate(14));
        evaluator.add(createDate(15));
        JCalendar jc = new JCalendar();
        jc.getDayChooser().addDateEvaluator(evaluator);
        jc.setCalendar(jc.getCalendar());
        f.add(jc);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private Date createDate(int d) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, d);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return (c.getTime());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new HighlightTest()::display);
    }
}

这篇关于Java在JCalendar单元格中突出显示特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆