1 54 55 package com.mullassery.act.gui.components; 56 57 import java.awt.BorderLayout ; 58 import java.awt.event.ActionListener ; 59 import java.awt.event.ItemListener ; 60 import java.util.Arrays ; 61 import java.util.HashMap ; 62 import javax.swing.DefaultComboBoxModel ; 63 import javax.swing.JButton ; 64 import javax.swing.JComboBox ; 65 import javax.swing.JPanel ; 66 67 import com.mullassery.act.gui.TextData; 68 69 74 public class TComboBox extends JPanel implements TextData { 75 private HashMap elements; 76 JComboBox jcb; 77 JButton ok; 78 79 80 public TComboBox(HashMap elements) { 81 this(elements, "Select"); 82 } 83 84 public TComboBox(HashMap elements, String buttonText) { 85 this.elements = elements; 86 this.setLayout(new BorderLayout ()); 87 88 DefaultComboBoxModel dcm = new DefaultComboBoxModel (getList()); 89 jcb = new JComboBox (); 90 jcb.setModel(dcm); 91 ok = new JButton (buttonText); 92 93 this.add(jcb, BorderLayout.CENTER); 94 this.add(ok, BorderLayout.EAST); 95 } 96 97 public void addActionListener(ActionListener il) { 98 ok.addActionListener(il); 99 } 100 101 public void addItemListener(ItemListener il) { 102 jcb.addItemListener(il); 103 } 104 105 public Object getItemValue(Object key) { 106 return elements.get(key); 107 } 108 109 private Object [] getList() { 110 Object [] keys = elements.keySet().toArray(); 111 Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER); 112 return keys; 113 } 114 115 public String getText() { 116 return "" + jcb.getSelectedItem(); 117 } 118 119 public void setText(String value) { 120 jcb.setSelectedItem(value); 121 } 122 } 123 | Popular Tags |