Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Detecting editable field entry in a JComboBox  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with a editable JComboBox I am hoping someone can help me with.

I have anumber of items in the ComboList and if a user wishes to select an item from the list they can either use the drop down list or just type the value in the editable field. However I want to know whether they have just entered the value or have selected from the list. Unfortunately it seems that methods such as getSelectedItem() will give the item in the list regardless of whether it is entered or selected. Any thought on how I can tell the difference?
 
Sheriff
Posts: 21421
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use getSelectedIndex() - it returns -1 if the value was not from the dropdown.
 
J R Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers - works a treat ....
 
J R Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I was to eager to say that this worked. If the item entered in the editable box is in the list then getSelectedIndex() does return the place in the list.
 
Author
Ranch Hand
Posts: 964
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why you want to distinguish between
the user choosing an item in the list and the user
typing in the very same value but, since you do,
there are ways to do it.

Probably easiest is to populate the combo box with
something other than String objects. For instance,
you could use StringBuffer objects instead.
 
J R Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - that works fine.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!