Last active
February 15, 2019 16:28
-
-
Save Ondkloss/b5c224d260d26eded0e355766c838ec5 to your computer and use it in GitHub Desktop.
JCalendar custom IDateEvaluator demonstration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.toedter.calendar.IDateEvaluator; | |
import java.awt.Color; | |
import java.util.Date; | |
import java.util.List; | |
public class SpecialDateEvaluator implements IDateEvaluator { | |
private final List<Date> specialDates; | |
public SpecialDateEvaluator(List<Date> specialDates) { | |
this.specialDates = specialDates; | |
} | |
@Override | |
public boolean isSpecial(Date date) { | |
for (Date d : specialDates) { | |
if (d.equals(date)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public Color getSpecialForegroundColor() { | |
return Color.black; | |
} | |
@Override | |
public Color getSpecialBackroundColor() { | |
return Color.red; | |
} | |
@Override | |
public String getSpecialTooltip() { | |
return null; | |
} | |
@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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.toedter.calendar.JCalendar; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.List; | |
import javax.swing.JFrame; | |
public class SpecialDateEvaluatorDemo { | |
public static void main(String[] args) throws ParseException { | |
List<String> mysqlDates = Arrays.asList("2019-02-14", "2019-03-06"); // Assume you've got this info somehow | |
List<Date> specialDates = convertToDates(mysqlDates); | |
JCalendar c = new JCalendar(); | |
c.getDayChooser().addDateEvaluator(new SpecialDateEvaluator(specialDates)); | |
c.setCalendar(Calendar.getInstance()); | |
JFrame f = new JFrame("Special dates"); | |
f.add(c); | |
f.pack(); | |
f.setVisible(true); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
} | |
public static List<Date> convertToDates(List<String> dateStrings) throws ParseException { | |
List<Date> dates = new ArrayList<>(); | |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | |
for (String dateString : dateStrings) { | |
dates.add(df.parse(dateString)); | |
} | |
return dates; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment