Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008, 02:23 PM
Member
 
Join Date: Jan 2008
Posts: 21
banie is on a distinguished road
HashMap and ComboBox
Hi,

I just new in java and i got to finish my project. But kind blur when to combine hash map and combobox. The variable that shown below need to give value. How to?

ImageShack - Hosting :: pilihcr0.png
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-25-2008, 06:52 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class MapCombo { private JPanel getContent() { final Map<String, Customer> map = getMap(); JComboBox combo = new JComboBox(); Set<String> keys = map.keySet(); Iterator<String> it = keys.iterator(); while(it.hasNext()) { String key = it.next(); combo.addItem(key); } combo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox combo = (JComboBox)e.getSource(); String key = (String)combo.getSelectedItem(); Customer customer = map.get(key); System.out.println(customer); } }); JPanel panel = new JPanel(); panel.add(combo); return panel; } private HashMap<String, Customer> getMap() { Customer[] customers = new Customer[4]; customers[0] = new Customer("Helen", "Oregon", 12); customers[1] = new Customer("Paul", "Idaho", 99); customers[2] = new Customer("Sue", "Texas", 22); customers[3] = new Customer("Oscar", "Utah", 42); HashMap<String, Customer> map = new HashMap<String, Customer>(); for(int i = 0; i < customers.length; i++) { String key = customers[i].name; Customer value = customers[i]; map.put(key, value); } return map; } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MapCombo().getContent()); f.setSize(200,100); f.setVisible(true); } } class Customer { String name; String location; int idNumber; Customer(String name, String loc, int id) { this.name = name; location = loc; idNumber = id; } public String toString() { return "Customer[name:" + name + " location:" + location + " idNumber:" + idNumber + "]"; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 12:58 AM
Member
 
Join Date: Jan 2008
Posts: 21
banie is on a distinguished road
Thank you bro. That very helpful.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList into hashMap Preethi New To Java 2 02-11-2008 09:13 AM
HashMap visual example jhetfield18 Advanced Java 1 12-12-2007 08:45 PM
Hashmap dirtycash New To Java 5 12-03-2007 03:58 AM
ComboBox with database options Goldy Advanced Java 0 12-01-2007 10:43 PM
what is hashmap gabriel New To Java 5 08-03-2007 02:23 PM


All times are GMT +3. The time now is 02:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org