I followed the codes here to set the colors of a specific date in Toedter's Calendar
. The problem I am facing now is that it is not highlighting the correct cell. In my example I have used 14th and 15th of June but it highlighted 8th and 9th.
And heres my code:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date sdate= null;
String d = null;
for(int i =0;i<pd.size();i++){
d = pd.get(i).getDate();
try{
sdate = (Date)formatter.parse(d);
if(events.contains(sdate)){
}
else{
events.add(sdate);
System.out.println(sdate);
}
}catch(ParseException r){
System.out.println("error");
}
}
//arraylist of events
for(int i = 0; i < events.size(); i++)
{
Calendar cal1 = Calendar.getInstance();
cal1.setTime(events.get(i));
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
int month1 = cal1.get(Calendar.MONTH);
int year1 = cal1.get(Calendar.YEAR);
//selected month and year on JCalendar
if(month == month1 && year == year1)
{
// 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;
component[day1 + offset ].setBackground(Color.blue);
}
}
As an alternative to changing the components, implement IDateEvaluator
and return the desired colors, as suggested here. It's not clear where your Calendar
offset goes awry. The example below uses List::contains
to identify special dates. Just be sure to clear the time fields on the calendar dates you add()
.
import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JCalendar;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
/**
* @see https://stackoverflow.com/a/37899883/230513
* @see https://stackoverflow.com/q/25501373/230513
*/
public class HighlightTest {
private static class HighlightEvaluator implements IDateEvaluator {
private final List<Date> list = new ArrayList<>();
public void add(Date date) {
list.add(date);
}
@Override
public boolean isSpecial(Date date) {
return list.contains(date);
}
@Override
public Color getSpecialForegroundColor() {
return Color.red.darker();
}
@Override
public Color getSpecialBackroundColor() {
return Color.blue;
}
@Override
public String getSpecialTooltip() {
return "Highlighted event.";
}
@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;
}
}
private void display() {
JFrame f = new JFrame("Highlight Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.add(createDate(14));
evaluator.add(createDate(15));
JCalendar jc = new JCalendar();
jc.getDayChooser().addDateEvaluator(evaluator);
jc.setCalendar(jc.getCalendar());
f.add(jc);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private Date createDate(int d) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, d);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return (c.getTime());
}
public static void main(String[] args) {
EventQueue.invokeLater(new HighlightTest()::display);
}
}