Toon posts:

[java] Implementeren interface werkt niet

Pagina: 1
Acties:

Verwijderd

Topicstarter
De volgende code heb ik rechtstreeks uit de java api docs gecopieerd, en ik krijg hem niet aan de praat:
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.

  • vinnux
  • Registratie: Maart 2001
  • Niet online
je return value is "this" en this is van het type JLabel && ListCellRenderer en al wat er boven hangt. Dat is dus niet gelijk aan Component.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
JComponent is een subklasse van een AWT Component, dus op dit punt klopt het wel degelijk.

Je code compileert hier gewoon keurig, dus als de compiler echt iets meldt, ligt het waarschijnlijk aan andere code?

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment