这是我的代码:

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


谁能帮我这个?

最佳答案

DefaultListModel只有一个不带参数的构造函数;您不能将所需的值作为构造函数arg传递。
您将必须创建DefaultListModel,然后在其后进行填充,例如:

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

关于java - 将HashMap转换为DefaultListModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9474479/

10-09 05:06