Home Segments Top Top Previous Next

1004: Mainline

The following is a renderer that checks the rating associated with the row in which a cell lies; if that rating is equal to or greater than a specified reference value, it renders the entire row in green to attract attention.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class MovieTableCellRenderer extends JLabel
                                    implements TableCellRenderer {
 private static int REFERENCE_VALUE = 25;                       
 public Component getTableCellRendererComponent(
   JTable table, Object value, boolean isSelected,
   boolean hasFocus, int row, int column) {
  setText(value.toString());
  setOpaque(true);
  setBackground(Color.white);
  if (value instanceof Integer) {
   setHorizontalAlignment(SwingConstants.RIGHT);
  }
  else if (value instanceof String) {
   setHorizontalAlignment(SwingConstants.LEFT);
  }
  Integer i = (Integer)(table.getModel().getValueAt(row, 1));   
  if (i.intValue() >= REFERENCE_VALUE) {                        
   setBackground(Color.green);                                  
  }                                                             
  else {                                                        
   setBackground(Color.white);                                  
  }                                                             
  return this; 
 } 
}