2012-02-28 29 views
0

下面是我的代码:的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

'DefaultListModel'不带任何参数,你传递一个集合。 – 2012-02-28 00:16:56

+0

有没有办法将hashmap中的每个值放入列表模型中? – cataschok 2012-02-28 00:20:00

+1

你需要添加元素到'ListModel',所以通过你的'HashMap'循环,并将每个值添加到'ListModel' – 2012-02-28 00:24:02

回答

1

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

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

谢谢!这非常有帮助 – cataschok 2012-02-28 10:53:04