Custom JComboBox with Autocompletion

Smaple JCombo with Auto-complete

Smaple JCombo with Auto-complete

———————————–SearchBoxModel.java———————————————

package com.example;

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 {
private static final long serialVersionUID = 1L;
ArrayList db = new ArrayList();
ArrayList data = new ArrayList();
String selection;
JComboBox cb;
ComboBoxEditor cbe;
int currPos = 0;

public SearchBoxModel(JComboBox jcb, ArrayList arr) {

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 = arr;
}

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());
}

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.toLowerCase()) || s.endsWith(in.toLowerCase())
|| s.contains(in.toLowerCase())) {
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) {

}
}

 

———————————-CustomJComboBox.java————————————————-

package com.example;

import java.util.ArrayList;

import javax.swing.*;

public class CustomJComboBox extends JComboBox
{
private static final long serialVersionUID = 1L;
private ArrayList arrList;
private SearchBoxModel sbm ;

public CustomJComboBox(ArrayList arrList)
{
super();
if(arrList!=null)
{
this.arrList = arrList;
}
else
{
this.arrList = initializeList();
}
sbm = new SearchBoxModel(this,this.arrList);
setModel(sbm);
addItemListener(sbm);
setSelectedItem("");
}

public ArrayList initializeList()
{
ArrayList arr = new ArrayList();
arr.add("romania");
arr.add("czech");
arr.add("luxemberg");
arr.add("brazil");
arr.add("canada");
arr.add("miami");
arr.add("los angeles");
arr.add("melbourne");
arr.add("london");
return arr;
}
}

 
—————————————-Main.java————————————————————–
package com.example;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

public class Main
{
private JFrame frame;
private JComboBox cbCategory;
private JTextField jtf;
private JPanel panel;

public Main()
{
frame = new JFrame();
frame.setBounds(100, 100, 220, 60);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

cbCategory = new CustomJComboBox(null);
cbCategory.setBounds(5, 5, 200, 20);
cbCategory.setEditable(true);
panel = new JPanel();
panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
panel.setBounds(0, 0, 210, 30);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.add(cbCategory);
frame.add(panel);
frame.setVisible(true);
frame.setEnabled(true);
}

public static void main(String args[])
{
Main m = new Main();
}
}

Rate this post

Leave a Reply