wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Date in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Date in JTable" Watch "Date in JTable" New topic
Author

Date in JTable

paritosh pandey
Greenhorn

Joined: Jan 08, 2008
Posts: 10
Is there is a way to restrict a jtable column to accept only date as input.
and format it to a specific format?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Originally posted by paritosh pandey:
Is there is a way to restrict a jtable column to accept only date as input.
and format it to a specific format?


The JTable pulls out data from the table model. JTable is just the view. It is bad usability to allow the user to edit in the table directly. Usually you present a dialog to the user, which he can use to edit single/multiple records. You can use a JFormatted text field or something similar to restrict and control the user input.

You can create customized renderers to display the date in the format of your choice or modify the getValueAt() method of your table model to return the correctly formatted string.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Originally posted by Maneesh Godbole:

...It is bad usability to allow the user to edit in the table directly...


I beg to differ - I'd say it's good usability to allow the user to edit in the table directly - people like Excel, after all, I think they'd get annoyed if you had to do any editing in a dialogue.

It does make our jobs as developers harder, but I think it is more user-friendly - no?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Originally posted by David Payne:


I beg to differ - I'd say it's good usability to allow the user to edit in the table directly - people like Excel, after all, I think they'd get annoyed if you had to do any editing in a dialogue.



I worked in a company previously where a PhD. Usability Expert was in charge of designing the UI. Like you I was of the same opinion. When I discussed this with him, his explanation was:
"Whenever the user is going to change anything, which might possibly affect different things, it is always advisable to present the user with an edit dialog. This is a visual clue to the user, that he is doing something which is not in line with the generic work flow. Also, the table cells might not be displaying all the information. So a dialog with all the information properly presented is always preferred."

Of course usability is extremly subjective, and I do not intend at all to fire of any debate. Once upon a time I had come accross a usability article by Sun. It would be interesting to take a look at their view on this. I will try to hunt it out and post the link.
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
I see your point, Maneesh, and was really only being awkward I guess I'd say that this is an example of where academic theory differs from what people want (or think they want).

It would definitely be 'better', in Excel to show all the facts about the cell you're editing - for example, whether the value that you're seeing now is a constant, or the result of some other formula, in which case you probably don't want to just delete that value. BUT, try making an application that works like that - people will ask why it can't work the same as Excel...

Sorry, paritosh, I've hijacked your thread completely unnecessarily.
Hafiz Yusof
Greenhorn

Joined: May 03, 2006
Posts: 4

Originally posted by Maneesh Godbole:

You can create customized renderers to display the date in the format of your choice or modify the getValueAt() method of your table model to return the correctly formatted string.


Because I'm using sorter in my table, so by format the date in getValueAt method, my sortable can't be sorted properly anymore

How can I format the date without affect sortable thing or wihtout convert the date to String ??
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Originally posted by Hafiz Yusof:


How can I format the date without affect sortable thing or wihtout convert the date to String ??


Use a renderer. Let the getValueAt return the Date object so that the sorting is not affected.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

These are the default renderer and editor used by JTable for Date.class:

You can recreate these classes but then just the way you want. The renderer is easy - just use your own (Simple)DateFormat. The editor can be customized a bit more though:

[ August 05, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hafiz Yusof
Greenhorn

Joined: May 03, 2006
Posts: 4
Thanks Rob and Maneesh.

Now by using Rob's approach, I can format the date and do sortable things happily ever after
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date in JTable