aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Detecting editable field entry in a JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Detecting editable field entry in a JComboBox" Watch "Detecting editable field entry in a JComboBox" New topic
Author

Detecting editable field entry in a JComboBox

J R Smith
Greenhorn

Joined: Oct 28, 2005
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

Use getSelectedIndex() - it returns -1 if the value was not from the dropdown.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
J R Smith
Greenhorn

Joined: Oct 28, 2005
Posts: 4
Cheers - works a treat ....
J R Smith
Greenhorn

Joined: Oct 28, 2005
Posts: 4
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

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
J R Smith
Greenhorn

Joined: Oct 28, 2005
Posts: 4
Thanks - that works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detecting editable field entry in a JComboBox