-3

How We create a textfield behave as autocomplete by onkeypress evnet in java as Database is Mysql.

Gray
  • 7,050
  • 2
  • 29
  • 52

1 Answers1

1

You should use a DocumentFilter (thanks MadProgrammer) instead of a KeyListener. The former is more reliable and easier to deal with.

Also instead of a JTextField you should use a JComboBox instead to show a drop down after the user begins typing.

Now I will not put the effort into creating some code for you since you did not show effort in asking the question.

Read up on:

jessechk
  • 450
  • 5
  • 15
  • 1
    I would not. I would suggest a `DocumentFilter` instead over a `DocumentListener`. If you try and modify a text field from within a `DocumentListener` you are likely to cause a exception of some sort (sorry can't remember the type). `DocumentFilter` will allow you to intercept the change before it's applied to the document and modify what is been added/removed – MadProgrammer Mar 25 '13 at 03:59
  • True, `DocumentFilter` is probably better in this situation. – jessechk Mar 25 '13 at 04:05