• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable and Custom TableCellRenderer - selection problem  RSS feed

 
Chrissy Simpson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone, I am using a custom TableCellRenderer to render a column in a JTable. This works great BUT when I select a column in the table, the column that is using the renderer is not highlighted. My custom renderer is pasted below. Does anyone know how I can highlight the JLabel if isSelected is true? Thanks.
class MyTableCellRenderer extends JLabel implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
setText(value.toString());
// more stuff here
return this;
}
}
 
Jeremy Maddera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the isSelected boolean the the method signiture and set the background you self...
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using j2se 1.4 or might ever going to use it, you should extend the DefaultTableCellRenderer not just implement the interface. this makes a big performance difference. see the API of DefaultTableCellRenderer and the source code (1.4!) for more information. moreover, inheritance might spare you some code concerning the selection problem.
cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!