• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

add listener to combo drop down list

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all, I have a JComboBox which contents change dynamatically. I need to perform some task ONLY when user selects item from the combo. I tried to add ActionListener and ItemListener, however this will fire action event even when the contents change. Then I tried to add MouseListener to the combo, but it only fired when user clicks on the "Text Field" of the combo (not when user clicks the arrow button and selects item from the list). How can I add the MouseListener to the drop down list of the combo so that I can detect ONLY when the user clicks? Pls guide me on this, thank you.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi akira,
I hope you get a reply, since I think that I have the same problem.
My problem is when I call method: myCombo.setSelectedIndex( int index ) it calls the action listener but I just want the action listener to be called when the user clicks on it.
I was thinking maybe to solve the problem by removing the actionListener ( myCombo.removeActionListener() ) before I use method: setSelectedIndex(), and after I set the right index I want selected, I will call method addAtionListener() so when the user click on the combobox it should call method actionPerformed().
I hope this will help you, I'm sure this is not the right way, but till somebody will give us some clue will have to go with this.
Yoel
------------------
Sun Certified Programmer for JAVA 2 Platform
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"akira"
you name doesn't agree with the javaranch guidelines.
please take a moment and re-register after reviewing the
guidelines at http://www.javaranch.com/name.jsp
thanks for your cooperation.
- satya
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi yoel,
I had found a solution, I tried it out and my problem was solved
I add the ItemListener to the combo box and check the ItemEvent (whether it is selected by the user). Here is a piece of sample code:

Hope this is useful for u!
cheers,
akira
ps: when posting source code, please use the
[ code ] and [ /code ]tags without the spaces.
thanks for changing you name.
- satya

[This message has been edited by Madhav Lakkapragada (edited August 24, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akira,
Thank you it you realy helped me out, I did as you posted using an ItemListener insted of a ActionListener, it works excellent.
Thanks Again
Yoel
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have exactly the same problem.
I have a panel, with table in the top and a panel with fields in the bottom. Whenever user selects the row in the table, the object of that row's data will be displayed in the bottom panel.
At this time the combobox data has been set. during this data population both ActionEvent and ItemEvents are fired. But I want to get the event specific to the user selection on this combobox, and not the event fired during dynamic population of the data.
I tried the same solution given in this thread, but it did not work.
I tried by getComponents and adding MouseListener in those components, which also did not work.
Please help me to solve the problem. Any help will be highly appreciated.
I'm using JDK1.3
Thanks
 
I don't always make ads but when I do they're tiny
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic