Skip to main content

JXDatePicker as table cell editor

3 replies [Last post]
aardvarkk
Offline
Joined: 2010-12-09
Points: 0

Hi there everybody!
I've been doing some research trying to figure out the best way to do a date selection inside a table cell. Currently, my table is a normal JTable. I started out by trying a method involving a popup containing the JXMonthView component (as suggested by Kleopatra in some forum messages Google turned up). It came very, very close to working. The problem was mainly around the component losing focus and not being 'removed' when focus was lost. Then I tried to do a modal dialog such as the Oracle JTable example for the color chooser. That went OK -- but a modal dialog really wasn't what I was looking for. When undecorated, you can't tell that it's modal because nothing flashes when you try to force it to 'lose focus'. And when it's decorated, it looks really bad popping up a window just to change a date. I'm really hoping for a solution that acts like a normal cell editor. Then I tried popping a JXDatePicker into the JTable as a cell editor, but the borders are completely messed up and the text becomes unreadable when I try to edit the date.
Is there a 'best practice' for date editing within a table? Is the best approach to use JXTable? Does it handle the JXDatePicker better than the JTable seems to? Or is it possible to use JXDatePickers inside normal JTables with a bit of tweaking? I'd really appreciate some kind of guidance to help point me in the right direction. I feel like the problem I'm trying to solve must have been solved many times before. I just can't seem to find a nice clean solution to doing it 'properly'.
Thanks!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

Use DatePickerCellEditor.
Karl

aardvarkk
Offline
Joined: 2010-12-09
Points: 0

Hey Karl,
Thanks so much for your help! I had to do a tiny bit of work because I use Joda-Time to represent my times (and thus I caused some errors with using the wrong types). All I did was to override the getValueAsDate() function to return a nice conversion between Joda-Time's DateTime type and the Java Date type. Also, I had a slightly different date format, which I solved by calling setFormats() inside my constructor. Looks and seems to work exactly as required. Thanks again!

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

I'm glad it worked. One thing that's been on my mind since JavaOne is JSR-310 (the spritiual successor to Joda-Time). Stephen said that the API was mostly stable, which is what we've been waiting for. If anyone is interested in working on a 310-compatible picker (conversion), let me know.
Karl