Search code examples
javaswingjtabletablecelleditorjcalendar

JDateChooser inside JTable cell enter key does not always work


I'm using slightly modified JDateChooserCellEditor class which allows me to put jDateChooser inside my jTable cell. Here is the code of class:

    public class JDateChooserCellEditor extends AbstractCellEditor implements
    TableCellEditor {


private JDateChooser dateChooser = new JDateChooser();

public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {

    Date date = null;
    if (value instanceof Date) {
        date = (Date) value;
    }
    dateChooser.setDateFormatString("dd-MM-yyyy");
    dateChooser.setDate(date);

    return dateChooser;
}

public Object getCellEditorValue() {

    dateChooser.setDateFormatString("dd-MM-yyyy");
    return dateChooser.getDate();
}

One thing does not work and I cannot find a solution. When I click for the first time on a cell that has jDateChooser inside, select date and hit enter key - nothing happens. The component maintains its focus but never confirms data. But if I after that select different cell the enter key magically works and date is saved to my jTable. After another try it does not work.. Next try - it works. It is so confusing. Thank you all for any help.


Solution

  • Well, I have found solution for my problem. I think its not the best way but it works and I have lost too much time trying to fix this. Adding listener to jDateChooser component and notifying to stop editing on property change as user kleopatra stated seems to solve the problem.

        dateChooser.addPropertyChangeListener(new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        if (evt.getPropertyName().equals("date")) {
                            stopCellEditing();
                        }
                    }
                });
    

    Thank you all for help.