Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date in JTable

 
paritosh pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Hafiz Yusof
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob and Maneesh.

Now by using Rob's approach, I can format the date and do sortable things happily ever after
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic