Originally posted by Jill Heaney:
Hi
I have a JTable that has a JDateChooser (toedter) in one of the columns. I have created a custom editor/renderer which sort of works, however I'm seeing 2 problems :
1. I can't remove a date (i.e. set it back to null)
2. I can't edit the date by typing into the field (this isn't too much of a problem)
Well I've never heard of "toedter JDateChooser" so take what I say here with a grain of salt, but presuming it doesn't do anything too weird perhaps I can help. Let me take your two issues in reverse order.
2. As far as editing the field via typing, you probably just need to call the spinner's commitEdit() method before getting its value. It would look something like this:
Except you probably have to catch the ParseException that commitEdit() might throw.
1. Setting a date "back to null" is trickier because spinners aren't designed to work this way. However,
you should be able to detect if the user has cleared out the field with something like this:
This won't work if com.toedter.calendar.JDateChooser doesn't use a subclass of JSpinner.DefaultEditor for it's editor component, which is possible. I'll leave it to you to check.
While I'm here, allow me to make a couple comments:
A. What does com.toedter.calendar.JDateChooser give you that you couldn't get from JSpinner alone? For example, If you changed the constructor of your cell editor to
and made the other necessary changes (change the type of the
theSpinner field to JSpinner, change calls to
theSpinner.get/setDate() to
theSpinner.get/setValue(), etc.) do you lose anything?
B. You could consider using a cell editor that uses a JFormattedTextField instead of a JSpinner. Both classes have the built-in date handling, and do things like allowing the user to increment/decrement dates using the arrow keys. But JFormattedTextField doesn't have those little arrow buttons.