0

I have a text box in which the user enters a unique number. After he enters a number and press tab or enter. I want to write query to search record matching to that unique number. The number is an integer. It will be passport no, pan card no or etc. If it is available in database, I want to display all information of that user.

I don't want to do that on button click.

I was looking for some TextChanged event but I found that the alternative in Java is the methods in document listener. But I am not getting how to use that. Also in Netbeans in the design view it does not show me any event on text changed. How to use Documentlistener to my JFrame/how to fix this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Anjali
  • 269
  • 2
  • 6
  • 13
  • Check out this question http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield – toniedzwiedz May 26 '12 at 10:36
  • 1
    If by 'text box' you mean a `JTextField`, add an `ActionListener`. It will respond to `Enter` at the very least. Activating on `Tab` is not the path of least surprise (for me at least). – Andrew Thompson May 26 '12 at 10:40
  • *"a unique number"* In what way 'unique'? Is the number an integer? – Andrew Thompson May 26 '12 at 10:41
  • It is an integer. It will be passport no, pan card no or etc. and If it is available into database, i want to display all information of that user. – Anjali May 26 '12 at 10:55
  • 5
    From your description of your problem, I don't see how a DocumentListener will help you, since this is for listening to changes in the document as they're being entered, and your problem description does not mention this need. Rather you state that you want to respond to the user pressing "enter" (add an ActionListener to the text component) or to the user leaving the text component via tab (add a FocusListener to the component). The two requirements are not solved by a DocumentListener. I think that before we start chasing wrong solutions, please clarify your problem. – Hovercraft Full Of Eels May 26 '12 at 11:11
  • @HovercraftFullOfEels --> +1; there must be a need for FocusListener (for Tab action) or an `InputMap`, `ActionMap` combo (Enter Key action)..or may be the need is not clear :) – Asif May 26 '12 at 14:52
  • @Anjali a passport or pan card number cannot be an `Integer` always. Please do recheck the length and size of these _numbers_ – Asif May 26 '12 at 14:53

1 Answers1

4

Here you can find a tutorial on DocumentListeners and example code as well.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hakan Serce
  • 11,198
  • 3
  • 29
  • 48