TableCellRender puzzle
To: comp.lang.java.gui
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
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24