TableCellRender puzzle  
Author Message
Roedy Green





PostPosted: 2008-1-12 21:18:00 Top

java-programmer, TableCellRender puzzle Below is a fairly simple TableCellRenderer for displaying an enum as
an Icon. That works fine. But I added a wrinkle, changing the
background colour depending on whether the row is selected.

No background colour changes happen, even though isSelected is true
when it should be. I wonder if I am missing something obvious.




package com.mindprod.vercheck;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

/**
* render a the AppSate enum cell, use icon without text.
*/
final class AppStateRenderer implements TableCellRenderer
{
// ------------------------------ FIELDS
------------------------------

private static final JLabel template = new JLabel( "",
JLabel.CENTER );

private Color background;

private Color selectedBackground;

// -------------------------- PUBLIC INSTANCE METHODS
--------------------------
/**
* constructor
*
* @param background background colour
* @param selectedBackground background colour when seleced
*/
public AppStateRenderer( Color background, Color
selectedBackground )
{
this.background = background;
this.selectedBackground = selectedBackground;
}

public Component getTableCellRendererComponent( JTable table,
Object value,
boolean
isSelected,
boolean hasFocus,
int row,
int column )
{
System.err.println( "selected:" + isSelected + " " + row + " "
+ table.getSelectedRow() );

template.setIcon( ( (AppState) value ).getIcon() );
template.setBackground( isSelected? selectedBackground :
background );

return template;
}
}
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Roedy Green





PostPosted: 2008-1-12 21:33:00 Top

java-programmer >> TableCellRender puzzle On Sat, 12 Jan 2008 13:18:26 GMT, Roedy Green
<email***@***.com> wrote, quoted or indirectly quoted
someone who said :

>final class AppStateRenderer implements TableCellRenderer

This is not sufficient. You must extend DefaultTableCellRenderer.
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com