Pratik Roy
Pratik Roy

Reputation: 734

Java GUI searching database

In my program I have created an excel sheet with many names. In a gui there is a text field. What i want is when user starts entering character(s), all the names beginning with those character(s) from the excel sheet should be pulled out and displayed. If the user enters more characters the list should change accordingly. Also user should be able to select one of those names with the mouse for further processing. eg : Excel Sheet Andy Angle Alice Australia John Jane

user starts entering 'a' output : Andy Angle Alice Australia

next letter 'an' output: Andy Angel and so on Thanks in advance and sorry if this is a basic question

Upvotes: 0

Views: 1058

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208964

  • To avoid having to search the file over and over, you'll want to store the data in some sort of data structure.
  • You'll also want to employ a DocumentListener to check for every time the document is change (i.e. a letter is typed or deleted from the text field
  • Depending on how you want the data displayed, you then would update the view's model accordingly base of the matched input in the text field.
  • A suggestion is maybe to use a JList.
  • Another option is to implement an auto-complete text field like exampled in this answer, that also uses the DocumentListener. I'm sure you can also search for other auto-complete Java implementations also.

Upvotes: 1

Related Questions