they changed my name
they changed my name

Reputation: 491

Accessing an outer class from inside a listener?

I have a listener inside Class A, and I want to pass Class A to my Class B inside the listener. Normally I'd just use this, but then I'd get the event that triggered the listener.

Upvotes: 6

Views: 2462

Answers (2)

twolfe18
twolfe18

Reputation: 2266

You can add a reference to class A in the constructor for the listener:

class_a_obj.addActionLister(new ActionListener(class_a_obj) { ...

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

A.this.

(It is rare that the inner class this is useful. Indeed it is relatively common to have bugs where the wrong this was used. So it is unfortunate that it is the default. Not about to change after 12 years.)

Upvotes: 9

Related Questions