Share on Google+Share on Google+

AutoSuggestion Box in Java Swing

In this tutorial you will come to know about the Auto Suggestion Box in Java Swing.

Auto Suggestion Box in Java Swing

In this section, you will learn how to create autosuggestion/autocomplete box in java swing. For this, we have created combo box and add array of countries using DefaultComboBoxModel to it. Now in order to show the combo box as autocomplete box, we have used keyListener class. As the user type the key, the countries that starts with that key will get displayed.

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class AutoSuggest extends JPanel{
    private final JTextField tf;
    private final JComboBox combo = new JComboBox();
    private final Vector<String> v = new Vector<String>();
    public AutoSuggest() {
        super(new BorderLayout());
        tf = (JTextFieldcombo.getEditor().getEditorComponent();
        tf.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
               EventQueue.invokeLater(new Runnable() {
            public void run() {
                String text = tf.getText();
                        if(text.length()==0) {
                    setModel(new DefaultComboBoxModel(v)"");
                    DefaultComboBoxModel m = getSuggestedModel(v, text);
                    if(m.getSize()==|| hide_flag) {
                        hide_flag = false;
                        setModel(m, text);
                public void keyPressed(KeyEvent e) {
               String text = tf.getText();
         int code = e.getKeyCode();
             if(code==KeyEvent.VK_ENTER) {
            if(!v.contains(text)) {
                setModel(getSuggestedModel(v, text), text);
            hide_flag = true
        }else if(code==KeyEvent.VK_ESCAPE) {
            hide_flag = true
        }else if(code==KeyEvent.VK_RIGHT) {
            for(int i=0;i<v.size();i++) {
                String str = v.elementAt(i);
                if(str.startsWith(text)) {
        String[] countries = {"Afghanistan""Albania""Algeria""Andorra""Angola","Argentina"
,"Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","Bulgaria",
"Burkina Faso"
,"Congo","Croatia","Cuba","Cyprus","Czech Republic","Denmark""Georgia",
,"Ghana","Great Britain","Greece","Hungary","Holland","India","Iran","Iraq",
,"Somalia""Spain""Sri Lanka""Sudan","Suriname""Swaziland","Sweden",
"Syria","Uganda","Ukraine","United Arab Emirates","United Kingdom",
"United States","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam",
          for(int i=0;i<countries.length;i++){
        setModel(new DefaultComboBoxModel(v)"");
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(BorderFactory.createTitledBorder("AutoSuggestion Box"));
        p.add(combo, BorderLayout.NORTH);
        setPreferredSize(new Dimension(300150));
    private boolean hide_flag = false;
       private void setModel(DefaultComboBoxModel mdl, String str) {
private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) {
        DefaultComboBoxModel m = new DefaultComboBoxModel();
        for(String s: list) {
            if(s.startsWith(text)) m.addElement(s);
        return m;
    public static void main(String[] args) {
       JFrame frame = new JFrame();
        frame.getContentPane().add(new AutoSuggest());

Output will be displayed as:

Download Source Code:




Posted on: October 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.