• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble making a custom cell renderer work in a JTable

 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

First off, you guys are awesome! I love this forum!

Now my issue is I have a JTable. The first column will contain Date objects all the way to the bottom. I want the JTable to display only the month and year. I created a custom cell renderer, but it appears the cell renderer method "getTableCellRendererComponent" is never called. (I tested it using System.out.println();) Here is my code for setting the cell renderer and the contents of the first column:



The Date objects are set into the cells of column 0 correctly. The table shows the result of the "toString()" method for each cell in that column.
If you need to see the code for the cell renderer I can post it. I am certain the problem is the cell renderer is not getting called though. Can you help please?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am certain the problem is the cell renderer is not getting called though


You need to override the getColumnClass() method of the TableModel to return Date.class so the table can use the appropriate renderer.

I created a custom cell renderer,


In case you are interested you can use the Table Format Renderer which allows you to do this easily. It also shows you an alternative way to assign the renderer to the table so you don't need to override the TableModel.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! Your suggestion works perfectly!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!