tolkinski
tolkinski

Reputation: 290

Populating JList from Hashmap and retriving key

How can I populate JList from a HashMap but to have only the value displayed in the list?

I am populating the list with following code. When I start the application I get list items like {1=Value1}, {2=Value2} etc. I want to display just the value.

I am using the HashMap because I need later when I submit the form where JList is to use the keys from values selected for my Insert method and I understood from other examples here that this is done with 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);
    }
}

This is My AdvertisementCategory model from where I want to export the data to 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;
}

}

Upvotes: 0

Views: 1703

Answers (1)

jarnbjo
jarnbjo

Reputation: 34323

You are somehow trying to implement a rather complex solution for a non-existing problem.

Using the MVC-principles of Swing, you would add the AdvertisementCategory directly to the list model and implement a ListCellRenderer to extract the display value from the AdvertisementCategory instance, which should be shown in the JList itself. Invoking getSelectedValue() on the JList will then give you the AdvertisementCategory instance and you have access to all its content.

Upvotes: 2

Related Questions