Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a JPanel as a column in a JTable

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want a JTable column to be a JPanel, do you need to write a
custom model to handle it? If so, how would that look?

Thanks.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not neccesarily, you may just want to write a custom cell renderer. What exactly are you trying to do ? What do you want to display in this column ?
D.
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each column in this table has different data with in it. For instance,
one column is titled Special Conditions with 6 check boxes in it. 3 on top
and 3 more below. It appears the only with to present this information is
in a JPanel, and the JPanel in a Table column.

Thanks.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So yes you need a custom renderer.

Have you written your model yet ? Your model, for that column, needs to return the data that you want to display. This could be for instance, an array of boolean (length 3). Or perhaps a class SpecialConditions that has 3 boolean attributes. It could also be lost of other things. I recommend creating a special class, like SpecialConditions.

You then need to create a custom cell renderer. This will implement TableCellRenderer (and possbly extend JPanel). It will have three checkboxes as attributes. In the getTableCellRendererComponent, you will set the checked property of the three checkboxes and return yourself.

You then need to specifiy your table to use your new renderer to render the correct data. You do this by either :
table.setDefaultRenderer method or
table.getColumn(index).setCellRenderer()

For reading round this subject you may want to check out "Custom cell renders and editors", in java swing tutorial on the Sun site.

hth, D.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic