Results 1 to 3 of 3
  1. #1
    banie is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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 + "]";
        }
    }

  3. #3
    banie is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •