1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing.components;
20
21 import java.awt.Color;
22 import java.awt.Component;
23
24 import javax.swing.JComponent;
25 import javax.swing.JTable;
26 import javax.swing.UIManager;
27 import javax.swing.table.DefaultTableCellRenderer;
28 import javax.swing.table.TableCellRenderer;
29
30 /***
31 * A TableCellRenderer that wraps another TableCellRenderer
32 * and sets the background to the specified color for odd-numbered rows.
33 * This makes every other row appear to be a different color,
34 * which helps users distinguish rows of data in densely-packed
35 * tables.
36 *
37 * @author michael@mpowers.net
38 * @author $Author: cgruber $
39 * @version $Revision: 904 $
40 */
41 public class AlternatingRowCellRenderer implements TableCellRenderer {
42
43 protected TableCellRenderer wrappedRenderer;
44 protected Color alternateColor;
45
46 /***
47 * Default constructor uses a lighter shade of the system control color
48 * and wraps a DefaultTableCellRenderer.
49 */
50 public AlternatingRowCellRenderer()
51 {
52 this( new DefaultTableCellRenderer() );
53 }
54
55 /***
56 * Uses the specified color for the background of the alternating rows,
57 * and wraps a DefaultTableCellRenderer.
58 */
59 public AlternatingRowCellRenderer(
60 Color aColor )
61 {
62 this( aColor, new DefaultTableCellRenderer() );
63 }
64
65 /***
66 * Uses the uses a lighter shade of the system control color
67 * for the background of the alternating rows,
68 * and wraps the specified TableCellRenderer.
69 */
70 public AlternatingRowCellRenderer(
71 TableCellRenderer aRenderer )
72 {
73 Color c = UIManager.getColor( "control" );
74 c = new Color(
75 (int) ( c.getRed() + ( ( 255 - c.getRed() ) / 1.5 ) ),
76 (int) ( c.getGreen() + ( ( 255 - c.getGreen() ) / 1.5 ) ),
77 (int) ( c.getBlue() + ( ( 255 - c.getBlue() ) / 1.5 ) ) );
78
79 alternateColor = c;
80 wrappedRenderer = aRenderer;
81 }
82
83 /***
84 * Uses the specified color for the background of the alternating rows,
85 * and wraps the specified TableCellRenderer.
86 */
87 public AlternatingRowCellRenderer(
88 Color aColor, TableCellRenderer aRenderer )
89 {
90 alternateColor = aColor;
91 wrappedRenderer = aRenderer;
92 }
93
94 public Component getTableCellRendererComponent(
95 JTable table, Object value,
96 boolean isSelected, boolean hasFocus,
97 int row, int column)
98 {
99 Component result = wrappedRenderer.getTableCellRendererComponent(
100 table, value, isSelected, hasFocus, row, column );
101 if ( ! isSelected )
102 {
103 if ( row % 2 == 0 )
104 {
105 if ( ! result.getBackground().equals( table.getBackground() ) )
106 {
107 result.setBackground( table.getBackground() );
108 }
109 }
110 else
111 {
112 if ( ! result.getBackground().equals( alternateColor ) )
113 {
114
115 if ( result instanceof JComponent )
116 {
117 ((JComponent)result).setOpaque( true );
118 }
119
120 result.setBackground( alternateColor );
121 }
122 }
123 }
124 return result;
125 }
126 }
127
128
129