wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes JTable cell to show time only and change based on time entered (only time, disregard date) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTable cell to show time only and change based on time entered (only time, disregard date)" Watch "JTable cell to show time only and change based on time entered (only time, disregard date)" New topic
Author

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

Geoff Berl
Greenhorn

Joined: Apr 13, 2011
Posts: 24

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

Joined: Oct 21, 2008
Posts: 1107

if all you want is the time, couldn't you simply store a string?
Geoff Berl
Greenhorn

Joined: Apr 13, 2011
Posts: 24

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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Geoff Berl
Greenhorn

Joined: Apr 13, 2011
Posts: 24

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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable cell to show time only and change based on time entered (only time, disregard date)
 
Similar Threads
Jtable as listbox?
Jtable cell editing question
JTable
getValueAt in JTable
Help updating cells in a JTable