4

This is for an assignment so responses should not contain the code written for me.

I have written a program that is essentially an auto-complete program. It takes a word and returns the best matches.

I am trying to write a front end for it in swing(which I have no experience in) and want my front end to do the following: I want the input box to constantly be reading for user input, feeding that value to the other program, and returning the matches immediately in a drop down box, as, say, Google does. I can't seem to find any information on how to do this, all the intro tutorials use a submit button.

Can anyone explain to me how this would be done, or point me to a resource that could explain it? Again, please don't write the code for me, I don't want to unwittingly cheat on my assignment.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
DazedAndConfused
  • 771
  • 3
  • 8
  • 19
  • search for AutoComplete, or Decorator, but in all cases good code is wrotten, long tine ago, and don't reinvent the wheel, those code are done by great minds ... – mKorbel Feb 24 '12 at 15:59
  • @mKorbel, good advice, however for an assignment, some artificial restrictions may be in place, esp. if the point of the assignment is this task... – Nim Feb 24 '12 at 16:00
  • The main focus of the assignment was the back end suggest algorithm, but all code, including that of any interface I use should be my own. – DazedAndConfused Feb 24 '12 at 16:27
  • 2
    +1 for showing an intent, not to cheat :-) – nIcE cOw Feb 24 '12 at 16:46

4 Answers4

6

If you are using a JTextField, you could register a document listener on it.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
  • Worth pointing to Oracle tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html – assylias Feb 24 '12 at 16:18
3

If your input box is a JTextField, you can add a DocumentListener (this is a good tutorial) to capture character entries.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

I think that no one from answerers ..., I'm only about Don't reinvent the wheel

1) use JTable with one (or two if is about Dictionary) Column and with basic implmentation for Sorting and Filtering (example with filtering from JTextField is in the Tutorial), JTable could be most complex from JComponents and there is everything (quite easilly) possible

2) use AutoComplete JComboBox / JTextField

3) use SwingX Decorator with JXList or JXTable

4) if you needed redirect output to the separate window then use JDialog / JWindow for popup window

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

One approach could be:

  1. Attach a handler to detect a key press on the text box.
  2. Grab the text from the box, and construct a "lookup" event which is runnable and submit this to some form of service which will dispatch it at some point in the future (hint: ExecutorService, Future)
  3. Save this handle, and if the key press event happens again, cancel the previous and submit a new one.
  4. When the event executes in the future and returns the result, popup a panel which displays the list of items.
Nim
  • 33,299
  • 2
  • 62
  • 101