Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

about jtextfield auto suggest

 
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..
 
Sheriff
Posts: 22015
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
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.
 
Hey! You're stepping on my hand! Help me tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic