将HashMap转换为DefaultListModel

时间:2012-02-28 00:14:59

标签: java hashmap defaultlistmodel

这是我的代码:

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

我收到错误:

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

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

DefaultListModel只有一个不带参数的构造函数;你不能将你想要的值作为构造函数arg传递。 您必须创建DefaultListModel,然后再填充它,例如:

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