JComboBox Key Event Selection

Posted on 2006-05-25
Medium Priority
Last Modified: 2009-12-16
I have a JComboBox that I want to respond to key events for selecting the item in the list.

For instance:

Vector people = new Vector();

JComboBox comboBox = new JComboBox(people);

When you select the Combo box and press the letter "S" it goes to Sam. However when you press "Saw", it does not go to Sawyer. Is there a way to do this? I wasn't sure what this technique is really called otherwise I would have googled it more.
Question by:Hokester

Accepted Solution

appxpete earned 500 total points
ID: 16763478
From: http://javaalmanac.com/egs/javax.swing/combobox_CbMultiKey.html

Selecting an Item in a JComboBox Component with Multiple Keystrokes

By default, when the user types a keystroke in a read-only combobox and an item in the combobox starts with the typed keystroke, the combobox will select that item. This behavior is not ideal if there are multiple items that start with the same letter.

This example demonstrates how to customize a combobox so that it will select an item based on multiple keystrokes. More specifically, if a keystroke is typed within 300 milliseconds of the previous keystroke, the new keystroke is appended to the previous keystroke, and the combobox will select an item that starts with both keystrokes.

    // Create a read-only combobox
    String[] items = {"Ant", "Ape", "Bat", "Boa", "Cat", "Cow"};
    JComboBox cb = new JComboBox(items);
    // Install the custom key selection manager
    cb.setKeySelectionManager(new MyKeySelectionManager());
    // This key selection manager will handle selections based on multiple keys.
    class MyKeySelectionManager implements JComboBox.KeySelectionManager {
        long lastKeyTime = 0;
        String pattern = "";
        public int selectionForKey(char aKey, ComboBoxModel model) {
            // Find index of selected item
            int selIx = 01;
            Object sel = model.getSelectedItem();
            if (sel != null) {
                for (int i=0; i<model.getSize(); i++) {
                    if (sel.equals(model.getElementAt(i))) {
                        selIx = i;
            // Get the current time
            long curTime = System.currentTimeMillis();
            // If last key was typed less than 300 ms ago, append to current pattern
            if (curTime - lastKeyTime < 300) {
                pattern += ("" + aKey).toLowerCase();
            } else {
                pattern = ("" + aKey).toLowerCase();
            // Save current time
            lastKeyTime = curTime;
            // Search forward from current selection
            for (int i=selIx+1; i<model.getSize(); i++) {
                String s = model.getElementAt(i).toString().toLowerCase();
                if (s.startsWith(pattern)) {
                    return i;
            // Search from top to current selection
            for (int i=0; i<selIx ; i++) {
                if (model.getElementAt(i) != null) {
                    String s = model.getElementAt(i).toString().toLowerCase();
                    if (s.startsWith(pattern)) {
                        return i;
            return -1;

Author Comment

ID: 16764694
Thanks! This looks EXACTLY like what I was looking for!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question