Reputation:
I'm using JCalendar
and have setup a listener using a PropertyChangeListener
. My problem is that this listener will not respond to an event on the current day, I assume because there is no change in the property. I want it to be able to respond to selecting today's date, as the calendar leads to a diary. When opened I still want the calendar to open on "today's" date, but to have a listener that will respond to pressing "today's" date. My code for the listener is below:
final JCalendar calendar = new JCalendar();
calendar.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if(Calendar.AddJob!=1){
JOptionPane.showMessageDialog(null,"No Entry Found");
frame.dispose();
String date = EditDate(calendar.getDate().toString());
WorkDiary.WorkDiaryGui(date);
}
if(Calendar.AddJob==1){
String date = EditDate(calendar.getDate().toString());
Calendar.AddJob=0;
frame.dispose();
WorkDiaryAddJob.WorkDiaryAddJobGui(CalReg, date);
}
}
});
Upvotes: 1
Views: 1972
Reputation: 17971
My problem is that this listener will not respond to an event on the current day, I assume because there is no change in the property.
Your assumption is correct: day chooser doesn't fire a day property change if you press the button for the very selected day (i.e. today). And it makes sense because the property doesn't change, actually.
I want it to be able to respond to selecting today's date, as the calendar leads to a diary.
To modify the aforementioned behavior we can use setAlwaysFireDayProperty(boolean alwaysFire) method to force day chooser to always fire a property change event:
JCalendar calendar = new JCalendar();
JDayChooser dayChooser = calendar.getDayChooser();
dayChooser.setAlwaysFireDayProperty(true); // here is the key
dayChooser.addPropertyChangeListener("day", ...);
Note: This is also explained in this answer.
Upvotes: 2
Reputation: 3454
i think you have to add an ActionListener instead of PropertyChangeListener...
it says: "JDayChooser is the ActionListener for all day buttons" (on ActionListener)
calendar.getDayChooser().addActionListener(new ActionListener(){
...
};
Upvotes: 0