JComboBox: getKeySelectionManager() : JComboBox « javax.swing « Java by API






JComboBox: getKeySelectionManager()

  



import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JComboBox;

public class Main {
  public static void main(String[] argv) throws Exception {
    String[] items = { "A", "A", "B", "B", "C" };
    JComboBox cb = new JComboBox(items);

    cb.addKeyListener(new MyKeyListener());
  }
}

class MyKeyListener extends KeyAdapter {
  public void keyPressed(KeyEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    int curIx = cb.getSelectedIndex();

    char ch = evt.getKeyChar();

    JComboBox.KeySelectionManager ksm = cb.getKeySelectionManager();
    if (ksm != null) {
      int ix = ksm.selectionForKey(ch, cb.getModel());
      boolean noMatch = ix < 0;
      boolean uniqueItem = ix == curIx;

      if (noMatch || !uniqueItem) {
        cb.showPopup();
      }
    }
  }
}

   
    
  








Related examples in the same category

1.implements JComboBox.KeySelectionManager
2.new JComboBox()
3.new JComboBox(ComboBoxModel aModel)
4.new JComboBox(Vector items)
5.JComboBox: addActionListener(ActionListener lis)
6.JComboBox: addItem(Object o)
7.JComboBox: addItemListener(ItemListener lis)
8.JComboBox: addPopupMenuListener(PopupMenuListener l)
9.JComboBox: getActionMap()
10.JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
11.JComboBox: getItemAt(int index)
12.JComboBox: getItemCount()
13.JComboBox: getSelectedIndex()
14.JComboBox: getSelectedItem()
15.JComboBox: insertItemAt(Object anObject, int index)
16.JComboBox: isPopupVisible()
17.JComboBox: removeAllItems()
18.JComboBox: removeItemAt(int anIndex)
19.JComboBox: setEditor(ComboBoxEditor anEditor)
20.JComboBox: setKeySelectionManager(KeySelectionManager aManager)
21.JComboBox: setRenderer(ListCellRenderer aRenderer)
22.JComboBox: setEditable(boolean b)
23.JComboBox: setMaximumRowCount(int count)
24.JComboBox: setSelectedIndex(int anIndex)
25.JComboBox: setSelectedItem(Object anObject)
26.JComboBox: setUI(ComboBoxUI ui)