2

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JavaNewb
  • 25
  • 1
  • 5
  • Seems ok. Please specify your question if there is something that you cant figure out, and need help with. – atomman Jan 06 '13 at 19:23
  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about `JFrame` with one `JComboBox` and hardcoded value in `HashMap, splitted (your view) to `MVC` – mKorbel Jan 06 '13 at 19:27
  • See also [*The Map Interface: Map Interface Basic Operations*](http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html). – trashgod Jan 06 '13 at 21:59
  • 1
    *"Here is the SSCCE."* 22Kb of Zip does not make 1 SSCCE! Make an example short enough to post as an edit to the question. – Andrew Thompson Jan 07 '13 at 00:06

1 Answers1

1

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.

ldam
  • 4,412
  • 6
  • 45
  • 76
  • 3
    The HashMap would iterate in unpredictable oder so entries in your ComboBox would always be randomly mixed. I do not know if this is a problem for you but maybe better to use TreeMap (would work with Integer keys iterating in sorted manner) or LinkedHashMap (would iterate in the order the keys have been added). – Audrius Meškauskas Jan 06 '13 at 19:52
  • Thank you. In this case, the problem I am having is basically how to iterate through the hashmap when the project is following MVC design. In your example, you add data and search through the data all from within one class. Instead, I need to write a method which can access the hashmap and return data which can be further used (presumably by another method) in order to add all returned items to the combobox in question. So, question is, how would these 2 methods look like (if that in fact is the correct way to do this). – JavaNewb Jan 06 '13 at 20:25
  • Like I said you can just pass the `HashMap` around in your program, or (probably a bad idea) make a static or public one that all your classes can access – ldam Jan 06 '13 at 21:09