/* Author: Jeff Dalton * Updated: Wed Nov 28 04:19:21 2001 by Jeff Dalton * Copyright: (c) 2001, AIAI, University of Edinburgh */ package ix.iface.util; import javax.swing.JComboBox; import java.awt.event.*; import ix.util.Debug; /** * An editable JComboBox that automatically adds any typed-in * items to the menu. */ public class ComboChoice extends JComboBox { public ComboChoice() { this(new Object[] {}); } public ComboChoice(Object[] items) { super(items); if (items.length == 0) this.addItem(""); this.setEditable(true); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ComboChoice c = (ComboChoice)e.getSource(); String selected = ((String)c.getSelectedItem()).trim(); Debug.noteln("ComboChoice selection", selected); if (!c.hasItem(selected)) c.addItem(selected); } }); } public boolean hasItem(Object item) { for (int i = 0, count = getItemCount(); i < count; i++) { if (getItemAt(i).equals(item)) return true; } return false; } public String getSelectedString() { return ((String)getSelectedItem()).trim(); } }