Jump to content
  • Advertisement
Sign in to follow this  
ju2wheels

Java: multiple action listeners

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have a JPanel with a JComoBox in it, and it has an action listener in it to perform certain tasks when the combobox has an item selected. I then add this panel to a frame. This frame also adds an action listener to the same combobox. Does this action event get eaten up by the panel's actionperformed method first and thus never reach the frame's actionperformed method until the second time around? I was wondering if there was something I had to specifically do to make sure they both got the action event.

Share this post


Link to post
Share on other sites
Advertisement
No, the component that triggers the events holds a list or similar to all action listeners. It's called "addActionListener", and not setActionListener. If you want, you can add 100 action listeners to it, and all 100 listeners will get triggered.

Toolmaker

Share this post


Link to post
Share on other sites
I think i just realized my problem... I thought that the first action listener to be added would be the one to be called first, however this isnt so.

Its the last action listener added that gets called first and then the next one in line in reverse order from that in which they were added.

Is there a way to reverse this order without actually changing the order in which i add them?

Share this post


Link to post
Share on other sites
I don't think that's possible. Why is it so important that you maintain a specific order? And if that order is so specific, why not define your own listener and let the Frame subscribe to that event? That way, you know for sure your Frame gets called last.

Toolmaker

Share this post


Link to post
Share on other sites
Quote:
Original post by ju2wheels
Its the last action listener added that gets called first and then the next one in line in reverse order from that in which they were added.


It is not a good idea to depend on this behavior. What if
they decide to use a hash table to store the listeners in the
next version? It will not be LIFO anymore.

Quote:

Is there a way to reverse this order without actually changing the order in which i add them?


Think about why you need to have the listeners notified in a
specifc order. Normally, you shouldn't care.

Share this post


Link to post
Share on other sites
Quote:
Original post by ju2wheels
Is there a way to reverse this order without actually changing the order in which i add them?

If processing order is important then you shouldn't be relying on the internal details of how such events are processed.

They're events therefore order shouldn't be important. If order is important you're really triggering two sequential actions from one event. Or your code is incorrectly assuming and order and you should fix it.

If you want two sequential actions from one event, write it like that. Have a single event handler which triggers the actions in the explicit ordering you need.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!