使用JCalendar在特定日期的背景色 [英] Background color on specific dates using JCalendar

查看:191
本文介绍了使用JCalendar在特定日期的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以不同的颜色在JCalendar中设置特定日期,具体取决于数据库中是否有针对该日期计划的日期,该日期在数据库中存储为"yyyy-MM-dd"在stackOverflow上有类似的帖子,但我无法使其正常工作.

Im trying to set specific dates in my JCalendar in a different color depending on if there is something planned for that date in my database, the date is stored as"yyyy-MM-dd" in the database, I've seen similar posts here on stackOverflow but I just cant get it to work.

我不确定"component [day] .setBackground(Color.green)"的工作方式,以及如何将其设置为仅在数据库中已为其计划好的日期的日期

I'm not sure how "component[day].setBackground(Color.green)" works like and how I can set it to only dates that has something planned for them in the database

    public void kalender() {
    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.DAY_OF_MONTH, 1);
    int offset = cal.get(Calendar.DAY_OF_WEEK);

    int mon = kalender.getMonthChooser().getMonth() + 1;

    int yr = kalender.getYearChooser().getYear();

    JPanel jPanel = kalender.getDayChooser().getDayPanel();

    Component component[] = jPanel.getComponents();

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
     String kalenderdatum = format.format(kalender.getDate());
     System.out.println(kalenderdatum);



    String sql2 = "SELECT DATUM FROM MOTE";
    try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(sql2);

        while (rs.next()) {
            String datumet = rs.getString("DATUM");



            String aret = datumet.substring(0, 4);
            int year = Integer.parseInt(aret);

            String manaden = datumet.substring(5,7);
            int month = Integer.parseInt(manaden);

            String dagen = datumet.substring(8,10);
            int day = Integer.parseInt(dagen);

            if(yr == year && mon == month)
            {

                component[day].setBackground(Color.green);
            }

            }

推荐答案

使用JCalendar API的一种解决方案是创建您自己的IDateEvaluator实例,并检查日期中是否包含特殊"内容.

One solution that uses the JCalendar API is to create your own instance of IDateEvaluator and checks if a date has anything "special" about it.

1.转换

首先,我建议将您的日期(yyyy-MM-dd)放入列表并将其转换为Date对象.例如:

First I suggest getting your dates (yyyy-MM-dd) into a list and convert them to Date objects. For example:

List<String> mysqlDates = Arrays.asList("2019-02-14", "2019-03-06"); // Assume you've got this info somehow
List<Date> specialDates = convertToDates(mysqlDates);

借助以下功能:

public static List<Date> convertToDates(List<String> dateStrings) throws ParseException {
    List<Date> dates = new ArrayList<>();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    for (String dateString : dateStrings) {
        dates.add(df.parse(dateString));
    }

    return dates;
}

2.创建您的SpecialDateEvaluator

然后,您需要创建自己的日期评估器,该评估器接受将要以不同方式处理的Date对象.一个简单的示例如下:

Then you need to create your own date evaluator that takes in the Date objects that are going to be handled differently. A simple example would be the following:

public class SpecialDateEvaluator implements IDateEvaluator {

    private final List<Date> specialDates;

    public SpecialDateEvaluator(List<Date> specialDates) {
        this.specialDates = specialDates;
    }

    @Override
    public boolean isSpecial(Date date) {
        for (Date d : specialDates) {
            if (d.equals(date)) {
                return true;
            }
        }

        return false;
    }

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

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

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

    @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;
    }
}

3.使用日期评估器

要使用评估器,您需要将其添加到JDayChooser,并获取Date对象的列表,然后再次设置Calendar以刷新视图.例如:

To utilize the evaluator you need to add it to the JDayChooser, taking in the list of Date objects and then setting the Calendar again to refresh the view. For example:

JCalendar c = new JCalendar();
c.getDayChooser().addDateEvaluator(new SpecialDateEvaluator(specialDates));
c.setCalendar(Calendar.getInstance());

要查看完整的示例(使用主要方法),请参见此示例要点.

To see a full example of this (with a main method), see this example gist.

这篇关于使用JCalendar在特定日期的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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