Adding actionListener to jCalendar

别来无恙 提交于 2019-12-19 04:10:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!