Getting Swing to read input without submit button

This is for assignment, so the answers should not contain code written for me.

I wrote a program that is essentially an autocomplete program. He takes the floor and returns the best matches.

I am trying to write the front end for it in swing (which I have no experience) and I want my interface to do the following: I want the input field to be constantly read for user input, passing this value to another program, and immediately return matches to drop-down list like, say, google. It seems I can’t find any information on how to do this, all introductory tutorials use the submit button.

Can someone explain to me how this will be done, or point me to a resource that could explain this? Again, please do not write the code for me, I do not want to unwittingly deceive my task.

+4
source share
4 answers

If you use JTextField , you can register a document listener .

+6
source

If your input field is a JTextField, you can add a DocumentListener (this is a good tutorial) to write character records.

+3
source

I think none of the defendants ... I'm just Don't reinvent the wheel

1) use JTable with one column (or two, if it is a dictionary) and the basic implication for Sorting and filtering (an example with filtering from JTextField is in the tutorial), JTable can be the most complex of JComponents and there is everything (pretty easy) maybe

2) use autocomplete JComboBox / JTextField

3) use SwingX Decorator with JXList or JXTable

4) if you need to redirect the output to a separate window, use JDialog / JWindow for the popup

+2
source

One approach could be:

  • Attach a handler to detect keystrokes in the text box.
  • Take the text from the window and create a β€œlookup” event that will be executed and send it to some kind of service that will send it at some point in the future (hint: ExecutorService , Future )
  • Save this handle, and if the keypress event happens again, undo the previous one and send a new one.
  • When an event is executed in the future and returns a result, a pop-up window that displays a list of items.
0
source

Source: https://habr.com/ru/post/1398255/


All Articles