-1

how to add key event listener in jcombobox so that it accepts the whole String for ex. if i add 'S' it should search for the String that start with 's' in database .if there are no result then it should wait for the user to type other character in the combobox to get the similar results

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user41531
  • 1
  • 1
  • 1
  • 3
    So now we have the requirements, what would your question be... – MadProgrammer Apr 18 '13 at 04:11
  • you need to write some code for your project I think – aymankoo Apr 18 '13 at 04:14
  • `if there are no result then it should wait for the user to type other character in the combobox` How the user knows that there are no results and he/she need to type another characetr? – Amarnath Apr 18 '13 at 04:42
  • and String can be inserted from Clipboard, then KeyListener doesn't react..., have to use Document, from derived JTextComponent from editable JComboBox, a few code examples here, maybe with autocompleted in DB – mKorbel Apr 18 '13 at 05:33

2 Answers2

1

Check if this helps-

    comboBox.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent ke) {
            // get the text typed and search for it
            // get the text from combo box, and not from the key event
        }
    });
Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
1

You have an option of either implementing it yourself or you can use Java2sAutoTextField or Java2sAutoComboBox. Both of them are easy to use.
You simply supply a java.util.List<> that contains data pulled from your database and that would do the task of auto completing for you.
To get a better idea, look at my question here: Implementing auto complete in Java - am I doing it right?

You also have another option of using GlazedLists
I believe I have adequately answered your question :)

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221