JCalendar set specific date colors

青春壹個敷衍的年華 提交于 2019-12-10 12:22:57

问题


I am using the code from the solution to set the colors of a specific date in toedter's JCalendar at Add specific background colors to JDaychooser Dates. The problem with this solution is that it sets a different day for each month because the first day for each month is different.

in my example i have added 4th of May and 4th of September in the events arraylist.+9 from the day works for May but in September it will select 7 instead because the first day of the month starts at +6.

I'm wondering if there is a way to get the start date for the month but i can't seem to find a method that does this in the API documentation.

Heres my code:

Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

JPanel jpanel = calendar.getDayChooser().getDayPanel();
Component component[] = jpanel.getComponents();

//arraylist of events
for(int i = 0; i < events.size(); i++)
{
    //selected month and year on JCalendar
    if(month == events.get(i).getMonth() && year == events.get(i).getYear())
    {
        //this value will differ from each month due to first days of each month
         component[ events.get(i).getDay() + 9 ].setBackground(Color.blue); 
    }
}

回答1:


What you need is to get the offset of the first day of the month. Analysing the calendar you know this is linked with the day of week.

Calendar cal = Calendar.getInstance();
cal.setTime(calendar.getDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

JPanel jpanel = calendar.getDayChooser().getDayPanel();
Component component[] = jpanel.getComponents();

//arraylist of events
for(int i = 0; i < events.size(); i++)
{
    //selected month and year on JCalendar
    if(month == events.get(i).getMonth() && year == events.get(i).getYear())
    {
         // 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;

        //this value will differ from each month due to first days of each month
         component[ events.get(i).getDay() + offset ].setBackground(Color.blue); 
    }
}

Does that make sense?




回答2:


I added a constant for the seven first objects of panel (Sunday to Saturday)

component[ events.get(i).getDay() + offset + 7].setBackground(Color.blue); 

and it worked for me




回答3:


Well A Simple Solution is that u have to get each Panel of Dare in Calender then you can easily change its color.

look following simple Example.

jPanel2 = jCalendar1.getDayChooser().getDayPanel();
Component component[] = jPanel2.getComponents();


  for (int i = 1; i <8 ; i++) {
         component[i].setBackground(Color.red);
    }

this will help.



来源:https://stackoverflow.com/questions/16704414/jcalendar-set-specific-date-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!