Search code examples
javaswingjcomboboxkeypress

JComboBox - Shorten KeyPress Listener


While the focus on a JComboBox, pressing keys in succession causes it to select the list item starting with those characters, in order. How would I shorten the amount of time allowed between each key press?


Solution

  • This is controlled in the BasicComboBoxUI class where you will find code like:

    Long l = (Long)UIManager.get("ComboBox.timeFactor");
    timeFactor = l == null ? 1000L : l.longValue();
    

    Which basically says the default is 1 second in order for keys pressed in succession to be concatenated together.

    To override this value you need to update the UIManager with your default value before you create your combo box:

    UIManager.put("ComboBox.timeFactor", new Long(500));
    JComboBox comboBox = new JComboBox( ... );