blob: d21fa979ac9cee631d9dd9dfddce28d75e0b233c (
plain)
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
|
package bjc.utils.gui;
import java.awt.Component;
import java.util.function.Function;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
/**
* Simple implementation of {@link ListCellRenderer} that uses a custom label.
*
* This renderer does the simple task of using a custom 'toString' method to display text for a given data type.
*
* @param <E> The data-type being displayed
*/
public class DelegateListCellRenderer<E> extends JLabel implements ListCellRenderer<E> {
private static final long serialVersionUID = -3312631037740011026L;
private Function<E, String> toStringer;
/**
* Create a new renderer using a given toString function
*
* @param toStringer The toString function to use
*/
public DelegateListCellRenderer(Function<E, String> toStringer) {
super();
this.toStringer = toStringer;
}
@Override
public Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected,
boolean cellHasFocus) {
String val = toStringer.apply(value);
setText(val);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
|