aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Question about 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 "Question about JCombobox" Watch "Question about JCombobox" New topic
Author

Question about JCombobox

Vineet Mishra
Greenhorn

Joined: Feb 21, 2008
Posts: 2
Hi , I have a JCombobox . I have attached one action listener to it. When I select something from the drop down , the action listener is called , which is I want . But if I
change something in the text area of the combo box , the action listener is also fired . which I dont want . So summery is : I want the action listener to be fired / called when
I change something in the drop down , not when I change something in the text box.

Any suggestion ??
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
It depends. If you don't want to handle typing to the combo box you should make it uneditable. If you change the combobox from code then you can remove the action listener every time before you change the value and add it again after you edited the value(or just use a variable to indicate that you are editing the value). I hope it helps.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Moving to Swing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to JavaRanch
Vineet Mishra
Greenhorn

Joined: Feb 21, 2008
Posts: 2
Hi Miklos, I need the text area of the combo box to be editable . Its a search functionality . use can enter something to search or they can choose from one from the search history displayed in the drop down .

Now removing and adding the listener I am currently doing but i can only do it when I am populating data in the combo box based on some other event like
another button click. But my problem is when the user changes data in the combo box by manually editing the existing one . and clicks something else , still the action listener is called.

Do I need to do something with jbutton.isFocusOwner() ???
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
My first thought was to check getSelecteIndex() in the action listener and based on that you can decide whether it is in the list or a user entered text. The only problem when you have a text "search this" in the combobox and the user also written "search this". In this case the selecedIndex will show the index of "search this". I hope somebody will show us the most elegant solution for this. Till then here's my version(I haven't tried yet, but I think it'll work): You make a class ComboBoxItem which holds a string and it's to string method returns the string. You put these items into the combobox instead of strings. In this way you can be sure that the getSelectedIndex() returns -1 when the user typed in the text and return the selected index when the user selectes something.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I advice you next time post code so anybody who would like to help can easily copypaste the code and play with it. It increases your chances to get help. I've tried my solution and it's working:



 
jQuery in Action, 2nd edition
 
subject: Question about JCombobox