JComboBox: adding automatic completion-Adding automatic selection : ComboBox « Swing Components « Java

JComboBox: adding automatic completion-Adding automatic selection

JComboBox: adding automatic completion-Adding automatic selection
//Code from: http://www.orbital-computer.de/JComboBox/
Inside JComboBox: adding automatic completion

Author: Thomas Bierhance

Adding automatic selection

To actually select an item in the combo box access to the combo box' model is needed inside 
our document. It can be passed as a parameter to the constructor (omitted here, see 
sourcecode). Adding some kind of automatic selection inside insertString...
import javax.swing.*;
import javax.swing.text.*;

public class S02BrokenAutoSelection extends PlainDocument {
    ComboBoxModel model;
    public S02BrokenAutoSelection(ComboBoxModel model) {
        this.model = model;
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        System.out.println("insert " + str + " at " + offs);
        // insert the string into the document
        super.insertString(offs, str, a);
        // get the resulting string
        String content = getText(0, getLength());
        // lookup a matching item
        Object item = lookupItem(content);
        // select the item (or deselect if null)
        if(item!=model.getSelectedItem()) System.out.println("Selecting '" + item + "'");
    private Object lookupItem(String pattern) {
        // iterate over all items
        for (int i=0, n=model.getSize(); i < n; i++) {
            Object currentItem = model.getElementAt(i);
            // current item starts with the pattern?
            if (currentItem.toString().startsWith(pattern)) {
                return currentItem;
        // no item starts with the pattern => return null
        return null;
    private static void createAndShowGUI() {
        // the combo box (add/modify items if you like to)
        JComboBox comboBox = new JComboBox(new Object[] {"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
        // has to be editable
        // get the combo boxes editor component
        JTextComponent editor = (JTextComponent) comboBox.getEditor().getEditorComponent();
        // change the editor's document
        editor.setDocument(new S02BrokenAutoSelection(comboBox.getModel()));
        // create and show a window containing the combo box
        JFrame frame = new JFrame();
        frame.pack(); frame.setVisible(true);
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {


Related examples in the same category

1.Swing Table in ComboBoxSwing Table in ComboBox
2.ComboBox color chooser (Windows Color Chooser)ComboBox color chooser (Windows Color Chooser)
3.MSN like Swing ComboBoxMSN like Swing ComboBox
4.Swing Auto Complete ComboBoxSwing Auto Complete ComboBox
5.Auto complete ComboBox
6.Stepped ComboBox ExampleStepped ComboBox Example
7.Block ComboBox ExampleBlock ComboBox Example
8.Disabled ComboBox ExampleDisabled ComboBox Example
9.ToolTip ComboBox ExampleToolTip ComboBox Example
10.ComboBox Menu ExampleComboBox Menu Example
11.Small Cell Combobox ExampleSmall Cell Combobox Example
12.JComboBox: adding automatic completion-Catching user inputJComboBox: adding automatic completion-Catching user input
13.JComboBox: adding automatic completion-Adding automatic completionJComboBox: adding automatic completion-Adding automatic completion
14.JComboBox: adding automatic completion-Fixed Auto SelectionJComboBox: adding automatic completion-Fixed Auto Selection
15.JComboBox: adding automatic completion-Case insensitive matchingJComboBox: adding automatic completion-Case insensitive matching
16.JComboBox: adding automatic completion-Prefer the currently selected itemJComboBox: adding automatic completion-Prefer the currently selected item
17.JComboBox: adding automatic completion-Ignore input that does not matchJComboBox: adding automatic completion-Ignore input that does not match
18.JComboBox: adding automatic completion-Highlight complete textJComboBox: adding automatic completion-Highlight complete text
19.JComboBox: adding automatic completion-Popup the item listJComboBox: adding automatic completion-Popup the item list
20.JComboBox: adding automatic completion-Cursor positionJComboBox: adding automatic completion-Cursor position
21.JComboBox: adding automatic completion-Handling the initial selectionJComboBox: adding automatic completion-Handling the initial selection
22.JComboBox: adding automatic completion-Handling focus lossJComboBox: adding automatic completion-Handling focus loss
23.JComboBox: adding automatic completion-BackspaceJComboBox: adding automatic completion-Backspace
24.JComboBox: adding automatic completion-Backspace 2JComboBox: adding automatic completion-Backspace 2
25.JComboBox: adding automatic completion-Pressing backspace at the beginningJComboBox: adding automatic completion-Pressing backspace at the beginning
26.JComboBox: adding automatic completion-Maximum MatchJComboBox: adding automatic completion-Maximum Match
27.JComboBox: adding automatic completion-Non-strict matchingJComboBox: adding automatic completion-Non-strict matching
28.JComboBox: adding automatic completion-Non-strict matching 2JComboBox: adding automatic completion-Non-strict matching 2
29.JComboBox: adding automatic completion-Binary Lookup and PerformanceJComboBox: adding automatic completion-Binary Lookup and Performance
30.JComboBox: adding automatic completion-Binay Lookup 2JComboBox: adding automatic completion-Binay Lookup 2
31.The KeyedComboBox model allows to define an internal key (the data element) for every entry in the model.
32.Auto Complete ComboBox 2
33.JLocaleChooser is a bean for choosing locales.