Please be more specific. In what way does it "not work"? Roedy's
suggestion is basically an exact copy of mine, and is correct, provided
you use the suggestion correctly.
Since you didn't post a proper code example showing what you tried,
there's no way to know for sure why it didn't work. But we can say for
sure that what you tried did not match either of the code examples I
provided in my first reply (not counting my goof of using "int" instead
of "Integer" as an example type parameter), since the techniques shown
in those examples _do_ work.
Pete
Example #1
import javax.swing.*;
public class KList<String> extends JList<E> {
private final DefaultListModel<String> model = new
DefaultListModel<String>();
public KList() {
setModel(model);
model.addElement("test");
}
}
C:\com\knutejohnson>javac KList.java
KList.java:3: error: cannot find symbol
public class KList<String> extends JList<E> {
^
symbol: class E
KList.java:8: error: method addElement in class DefaultListModel<E>
cannot be ap
plied to given types;
model.addElement("test");
^
required: String
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to
String by meth
od invocation conversion
where String,E are type-variables:
String extends Object declared in class KList
E extends Object declared in class DefaultListModel
2 errors
Example #2 Roedy's second example that he thought should work
import javax.swing.*;
public class KList<E> extends JList<E> {
private final DefaultListModel<E> model = new DefaultListModel<E>();
public KList() {
setModel(model);
model.addElement("test");
}
}
C:\com\knutejohnson>javac Klist.java
Klist.java:8: error: method addElement in class DefaultListModel<E#2>
cannot be
applied to given types;
model.addElement("test");
^
required: E#1
found: String
reason: actual argument String cannot be converted to E#1 by method
invocation
conversion
where E#1,E#2 are type-variables:
E#1 extends Object declared in class KList
E#2 extends Object declared in class DefaultListModel
1 error
Example #3 only gives a warning
import javax.swing.*;
public class KList extends JList {
private final DefaultListModel<String> model = new
DefaultListModel<String>();
public KList() {
setModel(model);
model.addElement("test");
}
}
C:\com\knutejohnson>javac KList.java
Note: KList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\com\knutejohnson>javac -Xlint:unchecked KList.java
KList.java:7: warning: [unchecked] unchecked call to
setModel(ListModel<E>) as a
member of the raw type JList
setModel(model);
^
where E is a type-variable:
E extends Object declared in class JList
1 warning
Example #4 Roedy's first suggestion compiles without warning but it is
no longer generic. Which brings me back to my real question, can you
extend a generic class and still be generic?
import javax.swing.*;
public class KList extends JList<String> {
private final DefaultListModel<String> model = new
DefaultListModel<>();
public KList() {
setModel(model);
model.addElement("test");
}
}