1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.identify;
25
26 import java.awt.Component;
27 import java.awt.Color;
28
29 import javax.swing.DefaultListCellRenderer;
30 import javax.swing.JLabel;
31 import javax.swing.JList;
32 import javax.swing.UIManager;
33
34
35
36
37
38
39
40 public class StripeListCellRenderer extends DefaultListCellRenderer {
41
42 private Color oddRowBackgroundColor = DEFAULT_ODD_ROW_BACKGROUND_COLOR;
43
44
45 public static final Color DEFAULT_ODD_ROW_BACKGROUND_COLOR = new Color(42, 87, 3, 12);
46
47
48
49
50
51
52 public final Color getOddRowBackgroundColor()
53 {
54 return oddRowBackgroundColor;
55 }
56
57
58
59
60
61
62
63
64 public final void setOddRowBackgroundColor(final Color oddRowBackgroundColor)
65 {
66 if (oddRowBackgroundColor == null)
67 {
68 throw new IllegalArgumentException("oddRowBackgroundColor must not be null");
69 }
70 Color oldOddRowBackgroundColor = this.oddRowBackgroundColor;
71 this.oddRowBackgroundColor = oddRowBackgroundColor;
72 firePropertyChange("oddRowBackgroundColor", oldOddRowBackgroundColor, this.oddRowBackgroundColor);
73 }
74
75 @Override
76 public Component getListCellRendererComponent(final JList list, final Object value, final int index, boolean isSelected, boolean hasFocus)
77 {
78 JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
79
80 if (isSelected)
81 {
82 label.setForeground(UIManager.getColor("List.selectionForeground"));
83 label.setBackground(UIManager.getColor("List.selectionBackground"));
84 }
85 else
86 {
87 label.setForeground(UIManager.getColor("List.foreground"));
88
89 if (index % 2 == 1)
90 {
91 label.setBackground(oddRowBackgroundColor);
92 }
93 else
94 {
95 label.setBackground(UIManager.getColor("List.background"));
96 }
97 }
98 return label;
99 }
100
101
102
103
104
105
106
107 public static <T> void install(final JList<T> list)
108 {
109 if (list == null)
110 {
111 throw new IllegalArgumentException("list must not be null");
112 }
113 StripeListCellRenderertml#StripeListCellRenderer">StripeListCellRenderer renderer = new StripeListCellRenderer();
114 list.setCellRenderer(renderer);
115 }
116 }