/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2001 Intersect Software Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.components; import java.awt.Color; import java.awt.Component; import javax.swing.JComponent; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; /** * A TableCellRenderer that wraps another TableCellRenderer and sets the * background to the specified color for odd-numbered rows. This makes every * other row appear to be a different color, which helps users distinguish rows * of data in densely-packed tables. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 904 $ */ public class AlternatingRowCellRenderer implements TableCellRenderer { protected TableCellRenderer wrappedRenderer; protected Color alternateColor; /** * Default constructor uses a lighter shade of the system control color and * wraps a DefaultTableCellRenderer. */ public AlternatingRowCellRenderer() { this(new DefaultTableCellRenderer()); } /** * Uses the specified color for the background of the alternating rows, and * wraps a DefaultTableCellRenderer. */ public AlternatingRowCellRenderer(Color aColor) { this(aColor, new DefaultTableCellRenderer()); } /** * Uses the uses a lighter shade of the system control color for the background * of the alternating rows, and wraps the specified TableCellRenderer. */ public AlternatingRowCellRenderer(TableCellRenderer aRenderer) { Color c = UIManager.getColor("control"); c = new Color( // lighten this color just slightly (int) (c.getRed() + ((255 - c.getRed()) / 1.5)), (int) (c.getGreen() + ((255 - c.getGreen()) / 1.5)), (int) (c.getBlue() + ((255 - c.getBlue()) / 1.5))); alternateColor = c; wrappedRenderer = aRenderer; } /** * Uses the specified color for the background of the alternating rows, and * wraps the specified TableCellRenderer. */ public AlternatingRowCellRenderer(Color aColor, TableCellRenderer aRenderer) { alternateColor = aColor; wrappedRenderer = aRenderer; } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component result = wrappedRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (!isSelected) { if (row % 2 == 0) { if (!result.getBackground().equals(table.getBackground())) { result.setBackground(table.getBackground()); } } else { if (!result.getBackground().equals(alternateColor)) { // jdk1.3's default renderer is opaque if (result instanceof JComponent) { ((JComponent) result).setOpaque(true); } result.setBackground(alternateColor); } } } return result; } }