• Post Reply Bookmark Topic Watch Topic
  • New Topic

about jtextfield auto suggest  RSS feed

 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.

im currently creating a JTextField that will be able to auto suggest words that are existing inside a vector..

i currently have this..


now im getting problems on checking if the currently entered characters would match anything inside the vector..
as you can see, i used the contains() method on vector. but it only checks for whole words.

i want to check if a certain object inside the vector somehow starts with the currently typed string.

i have an idea about using contains() with startsWith() but i dont have any idea how to do it..

please help..
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop through the list manually, using either an Iterator or the for-each loop. Then call startsWith on each separate element.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the documentation,
Class String wrote:Returns true if this vector contains the specified element. More formally, returns true if and only if this vector contains at least one element e such that (o==null ? e==null : o.equals(e)).

So one solution is to create a class called for example, Phrase. This class would have a member variable of type String that contains the phrase entered by the user. Instead of adding Strings to the Vector, add Phrase instances. Override the equals() method of the Phrase class and return true if the input String is in the member variable String. Please respond if you need further clarification.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
taking Mr. Prime's suggestion, i have come up to this..


i believe its working fine. now, im to put the suggested words to the text field and highlight the remainder of the currently possible matching word.

can you please guide me with that? thanks a lot.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!