0

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); 
    }
}
Community
  • 1
  • 1
user2273278
  • 1,275
  • 2
  • 14
  • 19
  • Alternatively, implement `IDateEvaluator` as shown [here](http://stackoverflow.com/a/37899883/230513). – trashgod Jun 19 '16 at 15:41

3 Answers3

2

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?

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Does that make sense?, yes but another of reasons why I used only code source not to extract methods or JComponents from compiled jar – mKorbel May 23 '13 at 07:35
0

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

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
Diego
  • 13
  • 5
0

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.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68