简体   繁体   中英

Iterate through hashmap and add items to swing jcombobox

I am working on my first Java application and I am stuck on this part where I need to populate my combobox with items from a hashmap.

I am using Model View Controller approach and trying to populate the combobox from within the GUI. So, after a user clicks on a button, a method gets called which should talk to the controller and request the items for the combobox. Controller should send back the items to the GUI and the combobox can be populated.

So, in one of my model files, I create a hashmap and add items to it via my controller.

The hashmap looks like this:

HashMap<Integer, Customer> customerRegisterHashMap = new HashMap<Integer, Customer>();

Let us assume that the map is now populated with sample data, according to the Customer class attributes.

Now, I guess that I need to implement a method in either the controller or the model itself which iterates through the hashmap above and returns the data (collection?).

I would (another assumption, may not be necessary), need too iterate through this data once more, only this time within the GUI class and add items to the combobox, one by one.

So the project looks like this: 5 files, Controller, Customerregister, Customer, Frame and a Application file which displays the Frame.

Thank you very much for any advice.

This may give you an idea:

HashMap<Integer, Customer> stuff = new HashMap<Integer, Customer>();
stuff.put(0, new Customer());
stuff.put(2, new Customer());
Iterator it = stuff.keySet().iterator();
while(it.hasNext())
{
    ComboBox.addItem(stuff.get(it.next()));
}

You can pass the HashMap around in your app and then just add things from it to the JComboBox as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM