Created
February 15, 2019 16:26
-
-
Save Ondkloss/7740ff1df111444a6086ae80f531d09e 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.time.DayOfWeek; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.List; | |
public class DayOfWeekEvaluator implements IDateEvaluator { | |
private final List<DayOfWeek> validDaysOfWeek; | |
public DayOfWeekEvaluator(List<DayOfWeek> validDaysOfWeek) { | |
this.validDaysOfWeek = validDaysOfWeek; | |
} | |
private DayOfWeek determineDayOfWeek(Date date) { | |
Calendar c = Calendar.getInstance(); | |
c.setTime(date); | |
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); | |
return DayOfWeek.of(convertSundayFirstToMondayFirst(dayOfWeek)); | |
} | |
private int convertSundayFirstToMondayFirst(int sundayFirstValue) { | |
// Converts from weeks that are Sunday-Saturday (1-7) | |
// to weeks that are Monday-Sunday (1-7) | |
if (sundayFirstValue == 1) { | |
return 7; | |
} | |
return sundayFirstValue - 1; | |
} | |
@Override | |
public boolean isSpecial(Date date) { | |
return false; | |
} | |
@Override | |
public Color getSpecialForegroundColor() { | |
return null; | |
} | |
@Override | |
public Color getSpecialBackroundColor() { | |
return null; | |
} | |
@Override | |
public String getSpecialTooltip() { | |
return null; | |
} | |
@Override | |
public boolean isInvalid(Date date) { | |
DayOfWeek dayOfWeek = determineDayOfWeek(date); | |
return !validDaysOfWeek.contains(dayOfWeek); | |
} | |
@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.time.DayOfWeek; | |
import java.util.Arrays; | |
import java.util.Calendar; | |
import java.util.List; | |
import javax.swing.JFrame; | |
public class DayOfWeekEvaluatorDemo { | |
public static void main(String[] args) { | |
List<DayOfWeek> validDaysOfWeek = Arrays.asList(DayOfWeek.MONDAY, DayOfWeek.THURSDAY); | |
JCalendar c = new JCalendar(); | |
c.getDayChooser().addDateEvaluator(new DayOfWeekEvaluator(validDaysOfWeek)); | |
c.setCalendar(Calendar.getInstance()); | |
JFrame f = new JFrame("Days of week"); | |
f.add(c); | |
f.pack(); | |
f.setVisible(true); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
} | |
} |
@DianaPrahoveanu see the DayOfWeekEvaluatorDemo.java
demonstration. You should be able to just run it.
@Ondkloss The problem is that I want to display/use this calendar in a existing jframe.Can you tell me is this is possible.If yes, how? Thank youuu
Is like I already have a GUI and I want to use it there
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I implement this in my jframe?