There is some library you can use such as SwingX or JIDE.
<dependency> <groupId>org.swinglabs</groupId> <artifactId>swingx</artifactId> <version>1.6.1</version> </dependency>
<dependency> <groupId>com.jidesoft</groupId> <artifactId>jide-oss</artifactId> <version>3.5.13</version> </dependency>
Or you can create it by learn this approach: http://www.orbital-computer.de/JComboBox/.
But all solution above, is Auto-complete. Like this:
Auto-complete will suggest item that start with some text.
But what I am try to discuss here, how to create Auto-suggestion JComboBox that suggest any item that start or end with some text. For example:
And how to create that one? We can’t just modified some library huh? Can we modified example from link http://www.orbital-computer.de/JComboBox/ above? May be you can modified from there but that example are too complicated for me.
While I’m googling, I found another approach here: http://www.badkernel.com/2011/06/java-swing-dynamic-combo-box/. Basically you can use this approach to create Auto-complete JComboBox. But if we took a look on method updateModel:
public void updateModel(String in) { data.clear(); // lets find any items which start with the string the user typed, and // add it to the popup list // here you would usually get your items from a database, or some other // storage... for (String s : db) if (s.startsWith(in)) data.add(s); super.fireContentsChanged(this, 0, data.size()); // this is a hack to get around redraw problems when changing the list // length of the displayed popups cb.hidePopup(); cb.showPopup(); if (data.size() != 0) cb.setSelectedIndex(0); }
On line 8, we see that if s start with in, then we add s to data. To create Auto-suggestion JComboBox, you can modify this line to:
if (s.contains(in))
So now you can use that class for Auto-suggestion JComboBox. Here a complete SearchBoxModel class that I’ve modified:
import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import javax.swing.AbstractListModel; import javax.swing.ComboBoxEditor; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; import javax.swing.JTextField; public class SearchBoxModel extends AbstractListModel implements ComboBoxModel, KeyListener, ItemListener { ArrayList<String> db = new ArrayList<String>(); ArrayList<String> data = new ArrayList<String>(); String selection; JComboBox cb; ComboBoxEditor cbe; int currPos = 0; public SearchBoxModel(JComboBox jcb) { cb = jcb; cbe = jcb.getEditor(); // here we add the key listener to the text field that the combobox is // wrapped around cbe.getEditorComponent().addKeyListener(this); // set up our "database" of items - in practice you will usuallu have a // proper db. db.add("aluminium"); db.add("iron"); db.add("iron oxide (2+)"); db.add("iron oxide (3+)"); db.add("sodium"); db.add("sodium chloride"); db.add("titanium"); db.add("selenium"); db.add("potassium"); db.add("polonium"); db.add("aluminium chloride"); } public void updateModel(String in) { data.clear(); // lets find any items which start with the string the user typed, and // add it to the popup list // here you would usually get your items from a database, or some other // storage... for (String s : db) if (s.contains(in)) data.add(s); super.fireContentsChanged(this, 0, data.size()); // this is a hack to get around redraw problems when changing the list // length of the displayed popups cb.hidePopup(); cb.showPopup(); if (data.size() != 0) cb.setSelectedIndex(0); } public int getSize() { return data.size(); } public Object getElementAt(int index) { return data.get(index); } public void setSelectedItem(Object anItem) { selection = (String) anItem; } public Object getSelectedItem() { return selection; } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { String str = cbe.getItem().toString(); JTextField jtf = (JTextField) cbe.getEditorComponent(); currPos = jtf.getCaretPosition(); if (e.getKeyChar() == KeyEvent.CHAR_UNDEFINED) { if (e.getKeyCode() != KeyEvent.VK_ENTER) { cbe.setItem(str); jtf.setCaretPosition(currPos); } } else if (e.getKeyCode() == KeyEvent.VK_ENTER){ cb.setSelectedIndex(cb.getSelectedIndex()); } else { updateModel(cb.getEditor().getItem().toString()); cbe.setItem(str); jtf.setCaretPosition(currPos); } } public void itemStateChanged(ItemEvent e) { cbe.setItem(e.getItem().toString()); cb.setSelectedItem(e.getItem()); } }
And how to use it? For example:
JComboBox cmbBox = new JComboBox(); cmbBox = new JComboBox(); cmbBox.setEditable(true); cmbBox.setBounds(140, 170, 180, 20); SearchBoxModel sbm = new SearchBoxModel(cmbBox); cmbBox.setModel(sbm); cmbBox.addItemListener(sbm); add(cmbBox);
And that how it works 🙂