Results 1 to 3 of 3
Thread: HashMap and ComboBox
- 03-25-2008, 01:23 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 21
- Rep Power
- 0
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
- 03-25-2008, 05:52 PM #2
Java 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 + "]"; } }
- 03-25-2008, 11:58 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
ArrayList into hashMap
By Preethi in forum New To JavaReplies: 2Last Post: 02-11-2008, 08:13 AM -
HashMap visual example
By jhetfield18 in forum Advanced JavaReplies: 1Last Post: 12-12-2007, 07:45 PM -
Hashmap
By dirtycash in forum New To JavaReplies: 5Last Post: 12-03-2007, 02:58 AM -
ComboBox with database options
By Goldy in forum Advanced JavaReplies: 0Last Post: 12-01-2007, 09:43 PM -
what is hashmap
By gabriel in forum New To JavaReplies: 5Last Post: 08-03-2007, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks