This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Custom DefaultTableCellRenderer not working as hoped for  RSS feed

 
Ranch Hand
Posts: 89
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some time I picked up a project I worked on. I'm an on and off type of developer, so my skills get rusty in time. Now I am stumped.

I have a JTable in which I record time worked on an activity. I wanted to offer the user the ability to say (s)he wanted to start working on an activity (by selecting a row). After some time (s)he could click on it again, and the application will calculate how much time has passed. That works like a charm. But when I write the new time to the JTable I get an java.lang.IllegalArgumentException: Cannot format given Object as a Date. The format renderer I use is based on Table Format Renderers from Rob Carmick.



And this is the logging I get:
setValue , value='2018-02-01' as read at start method
setValue , value='00:11:00' as read at start method
setValue '00:11:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date


I have been working on this problem for a few days now. I have tried numerous things, but cannot work it out. Hence my question. What am I doing wrong?

I had a discussion on this topic before: Field JTable not editable with (among others) Rob Camick.
 
Rancher
Posts: 2932
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your sscce demonstrating the problem. We can't tell how the data is actually store in the TableModel.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 89
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And


Logging when I run the application:
E:\Workspace\Test>java ProblemChild
setValue , value='00:10:00' as read at start method
setValue '00:10:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:33:00' as read at start method
setValue '00:33:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='01:15:00' as read at start method
setValue '01:15:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:10:00' as read at start method
setValue '00:10:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:33:00' as read at start method
setValue '00:33:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='01:15:00' as read at start method
setValue '01:15:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date


The object data I read in my actual program comes from a database. I works fine. I could enter time (worked on an activity) in the JTable of my application, which is then written to the database. I got the problem when I wanted to add functionality to record "working on an activity of a project". With a click on the row the recorded time is added to the time already on the activity, written to the database, and the table (model.setValueAt(some code)). This then generates the errors as mentioned above.
 
Rob Camick
Rancher
Posts: 2932
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The data in the TableModel needs to be added as a Date, not a String.

The point of using the custom renderers is to take a Date (or Integer or Double) and format the Object as a displayable String.

This is how I created the test data that is displayed in the image the the custom renderers blog:



The editor must then make sure a Date (or Integer or Double) Object is stored back in the model.

If you want a custom editor then I would suggest you can create a DefaultCellEditor using a JFormattedTextField as the parameter. Then you can have the formatted text field do the formatting with the mask your provide so you don't need to write the editor from scratch.


 
Daan Heuvelbeuk
Ranch Hand
Posts: 89
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. In the end I used a custom Time Editor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!