'Manual editing JFormattedTextField of JXDatePicker

I'm currently working on an issue I encountered using JXDatePicker. Indeed I've been able to implement a JXDatePicker that allows me to pick a date from the calendar pop up. The selected date is displayed thanks to a JFormattedTextField. This part works very well. The problem comes when user tries to edit the JFormattedTextField by hand to modify the date. As soon as the user tries to edit the date field, a refresh is done to quickly to be able to edit the date as wanted.

I think that the cause of this problem might be the default DocumentListener assigned to the JFormattedTextField called DocumentHandler. This DocumentHandler (implementing DocumentListener) calls the methods 'removeUpdate(DocumentEvent e)' and then 'insertUpdate(DocumentEvent e)' each time the user gets the JFormattedTextField focus.

The behavior I'm looking for about the JFormattedTextField is something like that:

  • Let the user modify the date field as he wants to
  • Wait for the user to press enter or leave focus of the JFormattedTextField to update the date
  • If the user puts a date that is not respecting the format, the previous value is set
  • Else, the new value is set

I thought about removing this DocumentListener and add an ActionListener and FocusListener instead to be able to listen to key press and unfocus, and then get the wanted behavior. The problem is I can't remove this DocumentListener because I must use a method 'removeDocumentListener(DocumentListener dl)', but there is no getDocumentListener() allowing me to call this method.

(I'm sorry about the english)

It is my first post here, I hope that I explained my issue in a understandable way, I'll be pleased if somebody has some piece of advice or even a solution :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source