cataschok cataschok - 5 months ago 17
Java Question

HashMap into DefaultListModel

Here's the code that I have:

HashMap<String, String> inst1 = new HashMap(instructorIO.getInstructors());
instListModel = new DefaultListModel<String>(inst1.values());


I get errors:

DepartmentProject.java:69: error: constructor DefaultListModel in class DefaultListModel<E> cannot be applied to given types;
required: no arguments
found: Collection<String>
reason: actual and formal argument lists differ in length
where E is a type-variable:
E extends Object declared in class DefaultListModel


Can anyone help me with this?

Answer

DefaultListModel only has a constructor that takes no arguments; you can't pass the values you want as a constructor arg. You will have to create the DefaultListModel and then populate it afterward, for example:

HashMap<String, String> inst1 = new HashMap(instructorIO.getInstructors());
instListModel = new DefaultListModel<String>();
for (String value : inst1.values())
{
    instListModel.addElement(value);
}
Comments