9

I'd like to detect when the date is changed in a JDateChooser field so that I can update another field.

Is this possible? And if so where should I be starting? I've already looked at the documentation and unfortunately there are no methods for adding something like an ActionListener or StateChangeListener (my first thoughts).

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
ghickman
  • 5,893
  • 9
  • 42
  • 51

2 Answers2

22

com.toedter.calendar.JCalendar inherits a listenerList from JComponent and it implements java.beans.PropertyChangeListener. I'd add a listener and see what comes though.

Edit: I think you can use addPropertyChangeListener() the same way JCalendar does.

JDateChooser chooser = new JDateChooser();
chooser.getDateEditor().addPropertyChangeListener(
    new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("date".equals(e.getPropertyName())) {
                System.out.println(e.getPropertyName()
                    + ": " + (Date) e.getNewValue());
            }
        }
    });
this.add(chooser);
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • 1
    +1 `com.toedter.calendar.JDateChooser` is a good example of doing this. – trashgod Nov 11 '10 at 20:31
  • @trashgod yes sorry I'm using the JDateChooser. I know it has an actionPerformed method but I'm pretty rusty with event handling in Java! I'm used to having an addXListener method to hand. How would I go about attaching a generic listener hook to that method? – ghickman Nov 19 '10 at 09:58
  • @ghickman: The example is right for the current version, 1.3.3; it looks like the posted API is 1.2.1. – trashgod Nov 20 '10 at 18:35
  • @trashgod: you're right! @Catalina Island: Thanks for the example, it's perfect! – ghickman Nov 22 '10 at 14:59
1

I've not tried it, but addDateListener(DateListener listener) looks appropriate.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • That does look good but unfortunately I'm not using that JCalendar (hadn't thought there might be two!). I'm using the toedter.com one: http://www.toedter.com/en/jcalendar/api/index.html – ghickman Nov 11 '10 at 16:17