• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable cell to show time only and change based on time entered (only time, disregard date)

 
Geoff Berl
Greenhorn
Posts: 24
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to come up with a way to have a column in my JTable display a date but in the format of only showing time (4:00 PM or 1600). It's for a time entry on a daily basis so I don't need the user entering the date. I also don't want the user to get a jumbled date time format when they double click the cell.

Can anyone point me in the right direction for some custom TableCellRenderer and/or CellEditor example that would do something like this?

All I seem to find on the web are Date/Time formats or if it's a time format displayed, once you double click the cell it shows you the whole date/time (Mon Mar 05 11:30:26 EST 2012) which is tedious to edit since the user should be able to just enter 1600 or 4PM and have it adjust the time.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if all you want is the time, couldn't you simply store a string?
 
Geoff Berl
Greenhorn
Posts: 24
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about just using a string but I wasn't sure if there was some Java format that would do it for me. If I use a string then I will need to develop the method which tries to convert the user's input into a valid result (input could be 1600, 4PM 4:00PM, 4 PM). I know that for dates Java already does the checks on it's own, for instance, I can write 12/22/2011 or 12/2011/22 and Java will automatically convert it to the set format since it is a Date object.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right that you need both a renderer and an editor. Those examples probably only used a custom renderer.

The renderer is the easiest. Take a look at the source of JTable (found in src.zip inside the JDK folder). Look for static class DateRenderer and see how it's implemented. You basically do the same but with a different DateFormat.

The editor is slightly more difficult. The default uses static class GenericEditor which uses toString() for displaying the value initially and the constructor that takes a String for parsing the value back. You need to change both. Let's take GenericEditor as a template and modify it as needed:
 
Geoff Berl
Greenhorn
Posts: 24
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is perfect, I was able to write my own Renderer but I was having trouble with the editor. This editor worked perfectly, thank you!
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. I do hope you get rid of all that commented code though I just left it there to see the differences with GenericEditor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic