3

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JLawrence
  • 192
  • 5
  • 22
  • See also [*How to include custom panel with NetBeans GUI Builder?*](http://stackoverflow.com/q/816286/230513) – trashgod Apr 12 '13 at 15:06

2 Answers2

8

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));   
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • or Changelistener or ItemListener works for me (could be MXxxListener in some versions) – mKorbel Apr 09 '13 at 17:01
  • it works with a JDayChooser but how do i get it to work on a JCalendar – JLawrence Apr 10 '13 at 18:27
  • Ah, I see; I've updated the answer based on a closer reading of the question. – trashgod Apr 10 '13 at 18:57
  • @JLawrence isn't there ChangeListener or MChangeListener, if yes then to use that (used old version, little bit modified based on codesource) – mKorbel Apr 11 '13 at 07:29
  • @mKorbel: I used [v1.4](http://www.toedter.com/en/jcalendar/), ~2011, but the [API on the web site](http://www.toedter.com/en/jcalendar/api/index.html) is ~2004. – trashgod Apr 12 '13 at 15:11
  • time to suck newer version, because I used only "modified code" and based code_source – mKorbel Apr 12 '13 at 15:14
  • 1
    @mKorbel: I only had two dependent projects: one upgraded cleanly and the other obviated an interim fix [cited here](http://stackoverflow.com/a/6824716/230513). – trashgod Apr 12 '13 at 15:27
  • Sorry this proved unacceptable; naturally, I'd like to learn of a better solution. – trashgod Apr 14 '13 at 16:42
  • i would like to clarify my question a bit more. My problem is with property change listener. since it triggers even when i change the month or year in a JCalendar. i would only like it to trigger when the day buttons are clicked( 1, 2,3...31). I thought that "day" would work in jc.addPropertyListener("day", new event listener) – JLawrence Apr 14 '13 at 16:48
  • nevermind. found a solution to that by using JDayChooser = jc.getDayChooser(); then using the code before the addendum – JLawrence Apr 14 '13 at 17:31
0

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());
   }
});
thirdy
  • 612
  • 8
  • 18