javaswingawtactionlistenerjcalendar

Adding actionListener to jCalendar


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


Solution

  • 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));   
        }
    });