Example usage for javax.swing JComboBox getKeySelectionManager

List of usage examples for javax.swing JComboBox getKeySelectionManager

Introduction

In this page you can find the example usage for javax.swing JComboBox getKeySelectionManager.

Prototype

public KeySelectionManager getKeySelectionManager() 

Source Link

Document

Returns the list's key-selection manager.

Usage

From source file:Main.java

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();//from  w  ww.  ja va2s .  com
        }
    }
}

From source file:Main.java

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) {
        // Determine if another item has the same prefix
        int ix = ksm.selectionForKey(ch, cb.getModel());
        boolean noMatch = ix < 0;
        boolean uniqueItem = ix == curIx;

        // Display menu if no matching items or the if the selection is not unique
        if (noMatch || !uniqueItem) {
            cb.showPopup();//from  ww w  .j a  v  a2s  .c o m
        }
    }
}