Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detecting editable field entry in a JComboBox

 
J R Smith
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
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.
 
Brian Cole
Author
Ranch Hand
Posts: 902
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic