Skip to main content

Bug in JXDatePicker - Keyboard Entry

4 replies [Last post]
miguelm
Offline
Joined: 2005-03-17
Points: 0

Comrades,
In the JSDatePicker, I can add an ActionListener to the component, which gets fired whenever the user pops up the calendar and changes the date. The trouble is, the user may just type a different date into the display field, without ever popping up the calendar. And if they do, I never get an ActionEvent sent to my listener. I looked for another kind of listener to add, but I couldn't find one. Am I missing something, or is this a bug?
-- Miguel Munoz

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
spinjava
Offline
Joined: 2007-06-12
Points: 0

Well, I used a DocumentListener on the Document of the JFormattedTextField that is part of JXDatePicker. You can track inserts and deletes, and it doesn't limit listening to hand editing, it also works fine when the user picks a date from the date picker. In that case, the date picker triggers several insert and delete events, but you can detect when the date is a valid date simply by evaluating if datePicker.getDate() != null.
Regards,
Francisco.

kschaefe
Offline
Joined: 2006-06-08
Points: 0

Why don't you listen for a property change on date?

Karl

miguelm
Offline
Joined: 2005-03-17
Points: 0

I'm not sure why I didn't think of that. Thanks.
I will say that it does leave me wondering what the purpose of the ActionListener is, if it only sometimes notifys me that the value has changed. (Maybe that's why I didn't think of the PropertyChangeListener.)

miguelm
Offline
Joined: 2005-03-17
Points: 0

Incidentally, here's my kludgy workaround:
<code>
// date picker uses an action listener when the change is made in the calendar pop-up
datePicker.getMonthView().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processChange();
}
});

// If the user changes the date by typing, it won't fire an ActionListener.
// So I detect the focus loss and fire the listener there. But this
// doesn't quite work, because the DatePicker hasn't yet seen the new date (possibly
// because it processes it in a subsequent focusLoss listener), so I delay the processing
// by calling invokeLater().
datePicker.getEditor().addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) { }

@Override
public void focusLost(FocusEvent e) {
if (!e.isTemporary()) {
// I can't fire processChange() yet because the date picker hasn't yet noticed the new date.
// So I delay the processing by invoking it later.
Runnable runnable = new Runnable() {
@Override
public void run() {
processChange(); // this reads the date from the JXDatePicker before processing it.
}
};
SwingUtilities.invokeLater(runnable); // delay the modelChanged
}
}
});
</code>