Java 从Hashmap填充JList并检索密钥
如何从HashMap填充JList,但只在列表中显示值 我正在用下面的代码填充列表。当我启动应用程序时,我会得到类似{1=Value1}、{2=Value2}等列表项。我只想显示值 我之所以使用HashMap,是因为我需要在稍后提交表单时,JList将使用为Insert方法选择的值中的键,我从这里的其他示例中了解到这是通过hashsmap完成的Java 从Hashmap填充JList并检索密钥,java,swing,hashmap,jlist,Java,Swing,Hashmap,Jlist,如何从HashMap填充JList,但只在列表中显示值 我正在用下面的代码填充列表。当我启动应用程序时,我会得到类似{1=Value1}、{2=Value2}等列表项。我只想显示值 我之所以使用HashMap,是因为我需要在稍后提交表单时,JList将使用为Insert方法选择的值中的键,我从这里的其他示例中了解到这是通过hashsmap完成的 public void populateListwithCategories(final JList list) { try {
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);
}
}
public void populateList with categories(最终JList列表){
试一试{
DefaultListModel listModel=新的DefaultListModel();
List advertisementCategories=AdvertisementCategorityProvider.getAdvertisementCategories();
对于(广告分类广告分类:广告分类){
int id=advertisementCategory.getId();
字符串名称=advertisementCategory.getName();
advertisementCategory.advertisementMap.put(id,name);
addElement(advertisementCategory.advertisementMap);
}
list.setModel(listModel);
}捕获(格式错误){
Logger.getLogger(AdvertisementCategoryController.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
Logger.getLogger(AdvertisementCategoryController.class.getName()).log(Level.SEVERE,null,ex);
}
}
这是我的AdvertisementCategory模型,我想从中将数据导出到hashmap
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;
}
公共类广告类别{
私有int-id;
私有字符串名称、描述;
公开列出广告类别;
public Map advertisementMap=新HashMap();
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共广告类别(int-id、字符串名称、字符串描述){
this.id=id;
this.name=名称;
this.description=描述;
}
公共广告类别(字符串名称、字符串描述){
this.name=名称;
this.description=描述;
}
公共广告类别(){
}
公共广告类别(int-id,字符串名称){
this.id=id;
this.name=名称;
}
}您试图为一个不存在的问题实施一个相当复杂的解决方案
使用Swing的MVC原则,您可以将AdvertisementCategory直接添加到列表模型中,并实现一个
ListCellRenderer
,以从AdvertisementCategory实例中提取显示值,该值应显示在JList中。在JList上调用getSelectedValue()
将为您提供AdvertisementCategory实例,您可以访问它的所有内容。没有,但是您可以自己使用Google,并找到大量好的教程。