user2273278
user2273278

Reputation: 1315

JCalendar set specific date colors

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.

enter image description here

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

Upvotes: 0

Views: 5195

Answers (3)

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

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.

Upvotes: 0

Diego
Diego

Reputation: 13

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

Upvotes: 0

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

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?

Upvotes: 2

Related Questions