问题
How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans?
I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser
P.S. using toedter's jCalendar
回答1:
Alternatively, you can listen for the specific propertyName
, "day"
.
JDayChooser jdc = new JDayChooser();
jdc.addPropertyChangeListener("day", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
Addendum: How do I get it to work on a JCalendar
?
Similarly, the propertyName
, "calendar"
represents a Calendar
from which you can get()
the DAY_OF_MONTH
.
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
});
回答2:
In case somebody misses reading the comments. Here's a sample working code.
JCalendar jCalendar = new JCalendar();
jCalendar.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
来源:https://stackoverflow.com/questions/15904824/adding-actionlistener-to-jcalendar