I am trying to create my first list, and I am stuck on an error.
Here is the template I am using for the list model:
private ListModel getListModel() {
String[] arrayOfStrings = new String[3];
arrayOfStrings[0] = "one";
arrayOfStrings[1] = "two";
arrayOfStrings[2] = "three";
ListModel listModel = new DefaultListModel();
for (int i=0;i<arrayOfStrings.length;i++) {
listModel.addElement(arrayOfStrings[i]);
}
}
The error:
error: cannot find symbol
listModel.addElement(arrayOfStrings[i]);
symbol: method addElement(String)
location: variable listModel of type ListModel
I am still new to using interfaces as well as lists. I downloaded an example code for making a list, and their code was very similar. What am I missing? I imported every single thing that the example code imported.
When in doubt, go to the API as it will show all. In this situation, the API will show you that the interface,
ListModel<E>
doesn't have anaddElement(...)
method. You're going to have to declare the variable as aDefaultListModel<E>
type, since that is the implementation that has this method.References:
ListModel<E>
APIDefaultListModel<E>
APIHaving said this, your method could still return a ListModel interface type... e.g.,