1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
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;
}
}
|