De volgende code heb ik rechtstreeks uit de java api docs gecopieerd, en ik krijg hem niet aan de praat:
Als fout melding krijg ik: They must have the same return type. Ik heb in de source gekeken van ListCellRenderer waarin precies dezelfde header voor de functie staat als in de code hierboven.
Java:
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
| class MyCellRenderer extends JLabel implements ListCellRenderer { final static ImageIcon longIcon = new ImageIcon("long.gif"); final static ImageIcon shortIcon = new ImageIcon("short.gif"); // This is the only method defined by ListCellRenderer. We just // reconfigure the Jlabel each time we're called. public Component getListCellRendererComponent( JList list, Object value, // value to display int index, // cell index boolean isSelected, // is the cell selected boolean cellHasFocus) // the list and the cell have the focus { String s = value.toString(); setText(s); setIcon((s.length() > 10) ? longIcon : shortIcon); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setEnabled(list.isEnabled()); setFont(list.getFont()); return this; } } |
Als fout melding krijg ik: They must have the same return type. Ik heb in de source gekeken van ListCellRenderer waarin precies dezelfde header voor de functie staat als in de code hierboven.