如何从 HashMap 填充 JList 但仅在列表中显示值?
我正在使用以下代码填充列表。当我启动应用程序时,我会得到 {1=Value1}、{2=Value2} 等列表项。我只想显示值。
我正在使用 HashMap,因为稍后我需要在提交 JList 的表单时使用为我的 Insert 方法选择的值中的键,并且我从此处的其他示例中了解到这是通过 hashsmap 完成的。
public void populateListwithCategories(final JList list) {
try {
DefaultListModel listModel = new DefaultListModel();
List<AdvertisementCategory> advertisementCategories = advertisementCategoryProvider.getAdvertisementCategories();
for (AdvertisementCategory advertisementCategory : advertisementCategories) {
int id = advertisementCategory.getId();
String name = advertisementCategory.getName();
advertisementCategory.advertisementMap.put(id, name);
listModel.addElement(advertisementCategory.advertisementMap);
}
list.setModel(listModel);
} catch (MalformedURLException ex) {
Logger.getLogger(AdvertisementCategoryController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AdvertisementCategoryController.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是我想将数据导出到 hashmap 的 My AdvertisementCategory 模型。
public class AdvertisementCategory {
private int id;
private String name, description;
public List<AdvertisementCategory> advertisementCategories;
public Map<Object, String> advertisementMap = new HashMap<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public AdvertisementCategory(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public AdvertisementCategory(String name, String description) {
this.name = name;
this.description = description;
}
public AdvertisementCategory() {
}
public AdvertisementCategory(int id, String name) {
this.id = id;
this.name = name;
}
}