How to Discover & remove ActionListeners

Y

Yin99

If ClassA has ActionListeners on JButtons, and ClassB instantiates
ClassA, can ClassB "discover" and remove all ActionListeners in ClassA?
(ClassA does not provide/pass any references to the Actionlisteners to
ClassB)


If you create a Component[] comps = this.getComponents(), and loop
through this calling getListeners, looks like you can discover
ActionListeners but this proposes several questions- 1) Will
this.getComponents() only work if called from ClassA (one with
actionlisteners, because of this reference?) 2) If this works to
discover actionlisteners, then how to remove?

Yin99
 
T

Thomas Weidenfeller

Yin99 said:
If ClassA has ActionListeners on JButtons, and ClassB instantiates
ClassA, can ClassB "discover" and remove all ActionListeners in ClassA?
(ClassA does not provide/pass any references to the Actionlisteners to
ClassB)

I would regard that as a very strange application design
If you create a Component[] comps = this.getComponents(), and loop
through this calling getListeners, looks like you can discover
ActionListeners but this proposes several questions- 1) Will
this.getComponents() only work if called from ClassA (one with
actionlisteners, because of this reference?) 2) If this works to
discover actionlisteners, then how to remove?

No you start to talk about Components instead of JButtons. What do you
really want?

There is no general API method to remove ActionListeners on Component
level. However, the components which allow ActionListener subscriptions
also have a method removeActionListener(). You would have to cast your
component to the proper Component subtype in order to call the method.

/Thomas
 
S

sanjay manohar

Quite.
And there are good reasons why you are not allowed to see what
listeners an object has, let alone remove them...

If you added the listeners yourself, then keep track of them yourself.
If someone else extends your code for some other purpose, they should
not tamper with your listeners, and you should not tamper with theirs!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top