Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about JCombobox

 
Vineet Mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Vineet Mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic