Mark Jcalendar when there is an event on the day

Asked

Viewed 445 times

0

In my Java Desktop application, I’m thinking of adding a Jcalendar to easily tell you what day events will be recorded in a database.

My question is whether it is possible to make an appointment or change of color in the calendar of the month.

1 answer

1


I believe you can use setBackground, follow an example:

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 dos eventos
for(int i = 0; i < events.size(); i++)
{
    //mês e ano selecionado em JCalendar
    if(month == events.get(i).getMonth() && year == events.get(i).getYear())
    {
         // Calcular o deslocamento do primeiro dia do mês
         cal.set(Calendar.DAY_OF_MONTH,1);
         int offset = cal.get(Calendar.DAY_OF_WEEK) - 1;

        //Este valor será diferente de cada mês, devido aos primeiros dias de cada mês
         component[ events.get(i).getDay() + offset ].setBackground(Color.blue); 
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.