Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JComboBox - autocomplete

807605Oct 3 2006 — edited Oct 9 2007
Hello everyone!

I wrote a piece of code that makes a JComboBox work autocomplete. It's working fine but not when I set the look and feel as a system look and feel - not java look and feel. If you're gonna read the code trying to help me, you're gonna see, that if the user types in the combo box some character, the text gets highlited. In the java look and feel it doesn't. So the highliting is only tisturbing the functionality of the combo box.

Any suggestions?

Thanks for future help!

The code:
import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;


class Testing extends JFrame {

    JComboBox jcb = null;
    Vector<String> v = null;

    public Testing() {
    
        this.getContentPane().setLayout(null);
        this.setLocation(300, 30);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        v = new Vector<String>();
        v.add("tlakovanje");
        v.add("tlakovec");
        v.add("tlakovana cesta");
        v.add("tla&#269;an");
        v.add("tlaka");
        v.add("slovenec");
        v.add("slovenka");
        v.add("slovenija");
        v.add("slovar");
        v.add("slovenci");
        v.add("&#269;ajra");
        v.add("&#269;aranje");
        
        jcb = new JComboBox();
        jcb.setEditable(true);
        jcb.setSelectedItem("");
        
        for (int i = 0; i < v.size(); i++) {
            jcb.addItem(v.get(i));
        }
        
        jcb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() != 38 && e.getKeyCode() != 40 && e.getKeyCode() != 10) {
                String a = jcb.getEditor().getItem().toString();
                jcb.removeAllItems();
                int st = 0;
            
                for (int i = 0; i < v.size(); i++) {
                    if (v.get(i).startsWith(a)) { jcb.addItem(v.get(i)); st++; }
                }
                jcb.getEditor().setItem(new String(a));
                jcb.hidePopup();
                if (st != 0) { jcb.showPopup(); }
            }
        } } );
        
        jcb.setBounds(20, 20, 150, 20);
        this.getContentPane().add(jcb);
        
        this.setVisible(true);
        
    }

    public static void main(String[] args) {
        UIManager.LookAndFeelInfo[] l = UIManager.getInstalledLookAndFeels();
        try {
            UIManager.setLookAndFeel(l[0].getClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Testing testing;
        testing = new Testing();
    }
    
}
Tilen
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2007
Added on Oct 3 2006
4 comments
2,439 views