Java | Auto complete or search in a JComboBox Code Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

Auto complete or search in a JComboBox

import java.awt.Component;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.text.*;

public class AutoCompleteComboBox extends JComboBox
{
private static final Locale[] INSTALLED_LOCALES = Locale.getAvailableLocales();
private ComboBoxModel model = null;
public static void main(String[] args)
{
JFrame f = new JFrame(“AutoCompleteComboBox”);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AutoCompleteComboBox box = new AutoCompleteComboBox(INSTALLED_LOCALES, false);
f.getContentPane().add(box);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
/** * Constructor for AutoCompleteComboBox –
* The Default Model is a TreeSet which is alphabetically sorted and doesnt allow
duplicates. * @param items */
public AutoCompleteComboBox(Object[] items, boolean caseSensitive)
{
super(items);
model = new ComboBoxModel(items);
setModel(model);
setEditable(true);
setEditor(new AutoCompleteEditor(this, caseSensitive));
}
/** * Constructor for AutoCompleteComboBox –
* The Default Model is a TreeSet which is alphabetically sorted and doesnt allow
duplicates. * @param items */
public AutoCompleteComboBox(Vector items, boolean caseSensitive)
{
super(items);
model = new ComboBoxModel(items);
setModel(model);
setEditable(true);
setEditor(new AutoCompleteEditor(this, caseSensitive));
}
/** * Constructor for AutoCompleteComboBox –
* This constructor uses JComboBox’s Default Model which is a Vector.
* @param caseSensitive */
public AutoCompleteComboBox(boolean caseSensitive)
{
super();
setEditable(true);
setEditor(new AutoCompleteEditor(this, caseSensitive));
}

/* * ComboBoxModel.java */
public class ComboBoxModel extends DefaultComboBoxModel
{
/** * The TreeSet which holds the combobox’s data (ordered no duplicates)
*/
private TreeSet values = null;
public ComboBoxModel(List items)
{
super();
this.values = new TreeSet();
int i, c;
for (i = 0, c = items.size(); i < c; i++)
values.add(items.get(i).toString());
Iterator it = values.iterator();
while (it.hasNext())
super.addElement(it.next().toString());
}
public ComboBoxModel(final Object items[])
{
this(Arrays.asList(items));
}
}

/* * AutoCompleteEditor.java */
public class AutoCompleteEditor extends BasicComboBoxEditor
{
private JTextField editor = null;
public AutoCompleteEditor(JComboBox combo, boolean caseSensitive)
{
super();
editor = new AutoCompleteEditorComponent(combo, caseSensitive);
}
/**
* overrides BasicComboBox’s getEditorComponent to return custom TextField
(AutoCompleteEditorComponent) */
public Component getEditorComponent()
{
return editor;
}
}

/* * AutoCompleteEditorComponent.java */
public class AutoCompleteEditorComponent extends JTextField
{
JComboBox combo = null;
boolean caseSensitive = false;
public AutoCompleteEditorComponent(JComboBox combo, boolean caseSensitive)
{
super();
this.combo = combo;
this.caseSensitive = caseSensitive;
}
/**
* overwritten to return custom PlainDocument which does the work*/
protected Document createDefaultModel()
{
return new PlainDocument()
{
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
if (str == null || str.length() == 0)
return;
int size = combo.getItemCount();
String text = getText(0, getLength());
for (int i = 0; i < size; i++)
{
String item = combo.getItemAt(i).toString();
if (getLength() + str.length() > item.length())
continue;
if (!caseSensitive)
{
if ((text + str).equalsIgnoreCase(item))
{
combo.setSelectedIndex(i);
//if (!combo.isPopupVisible())
// combo.setPopupVisible(true);
super.remove(0, getLength());
super.insertString(0, item, a);
return;
}
else if (item.substring(0, getLength() + str.length()).equalsIgnoreCase(text + str))
{
combo.setSelectedIndex(i);
if (!combo.isPopupVisible())
combo.setPopupVisible(true);
super.remove(0, getLength());
super.insertString(0, item, a);
return;
}
}
else if (caseSensitive)
{
if ((text + str).equals(item))
{
combo.setSelectedIndex(i);
if (!combo.isPopupVisible())
combo.setPopupVisible(true);
super.remove(0, getLength());
super.insertString(0, item, a);
return;
}
else if (item.substring(0, getLength() + str.length()).equals(text + str))
{
combo.setSelectedIndex(i);
if (!combo.isPopupVisible())
combo.setPopupVisible(true);
super.remove(0, getLength());
super.insertString(0, item, a);
return;
}
}
}
}
};
}
}
}