LGoodDatePicker / LGoodDatePicker

Java Swing Date Picker. Easy to use, good looking, nice features, and localized. Uses the JSR-310 standard.
MIT License
265 stars 93 forks source link

After invalid character in year does not reset to previous value like day and month do #178

Open dbmalkovsky opened 2 years ago

dbmalkovsky commented 2 years ago

Start with a date of 04/03/2022: image

Now replace the second 2 with a; the value is highlighted correctly:

image

Now lose the focus and the resulting year is now: 0202: image

It appears that the code removes the "column" with the invalid value and adds a leading zero. Thus if you change the last 2 to a; lose focus the value is now 0020. If you repeat it again (that is change the remaining 2 to a and lose focus) the result is April 3, 0.

image

If one adds extra characters (instead of replacing a column) the value reverts to the previous value as expected.

This behavior doesn't seem to hold for days or month values.

I've spent some time debugging and haven't found exactly where this is happening when focus is lost.

dbmalkovsky commented 2 years ago

I have spent more hours than I care on this and I think I know what is happening and have come up with a "work around".

The issue with changing the year is that there is a text change listener in the DatePicker that fires zEventTextFieldChanged() for inserts, removes, and changes. So when editing the year to say change 2022 to 20a2, the remove event is fired and the year value of 202 is parsed as the year; which is valid, and thus the new default fall back year is set to 0202. The a is then inserted and the event is fired again and the value 20a2 is not a valid year so it reverts to the most recent saved year which is 0202.

My workaround to this is to set the date range for the field to require a value after the year 999 as follows: dateSettings.setDateRangeLimits(LocalDate.of(999, 12, 31), null);

Not great as when the value is before the year 999 it strikes through the value (the default veto action) instead of highlighting it in red.