I'm having trouble with highlighting today's date on the JCalendar. I tried to look for answers here and online in general but I couldn't seem to find one. For context, a feature of my program allows the user to add an assignment with a due date into their SQL database. I did this so that the JCalendar will highlight certain dates that belong to the user's assignments. This is the code:
import com.toedter.calendar.IDateEvaluator;
import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import java.awt.Color;
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class calendar {
private static class Evaluator implements IDateEvaluator {
private List<Date> assignmentDates = new ArrayList<>();
public void add(Date dates) {
assignmentDates.add(dates);
}
public void remove(Date date) {
assignmentDates.remove(date);
}
public void setDates(List<Date> dates) {
assignmentDates.addAll(dates);
}
@Override
public boolean isSpecial(Date date) {
return assignmentDates.contains(date);
}
@Override
public Color getSpecialForegroundColor() {
return Color.WHITE;
}
@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;
}
@Override
public Color getSpecialBackroundColor() {
return Color.MAGENTA;
}
}
private void display() {
List<Date> datesList = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// example of the date I stored in my database
try {
datesList.add(dateFormat.parse("2025-03-06"));
datesList.add(dateFormat.parse("2025-03-24"));
}
catch (Exception ex) {
ex.getMessage();
}
JDateChooser dateChooser = new JDateChooser();
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
JFrame f = new JFrame("Calendar");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCalendar jc = new JCalendar();
for (int i = 0; i < datesList.size(); i++) {
dates.add(createDate(datesList.get(i)));
}
Evaluator evaluator = new Evaluator();
evaluator.setDates(dates);
jc.getDayChooser().addDateEvaluator(evaluator);
jc.setCalendar(jc.getCalendar());
f.add(jc);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
jc.repaint();
}
private Date createDate(Date d) {
Calendar c = Calendar.getInstance();
c.setTime(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) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new calendar()::display);
}
}
However, when the user sets the due date of an assignment to today, the date in the JCalendar is not highlighted.
Can you help me figure out why? and how to fix this?
You're running into a few issues all at once.
The JDayChooser defaults to setting the selected date to 'today', which in the tests case is also a 'special' date:
datesList.add(dateFormat.parse("2025-03-06"));.
There's some ambiguity as to how to paint a specific day. Is it selected? Is it a weekend? Is it 'special'? Which color should prevail?
There's a bug in the JDayChooser such that it does not keep track of which alternative paint is being used. When the "selected date" is changed, it just restores the default paint instead of today or special. This is why, when you select 3/24, then select another day, it does not paint in pink anymore.
I'd say it's beyond the scope of this question to fix all the bugs in JCalendar. For what it's worth, the component was abandoned and there are forks on github. I suspect they have the same bugs.