Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the super class of TreeSelectionListener in Java?

this.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {

            // How do I access the parent tree from here?           
        }           
    });
like image 794
Goutham Avatar asked Nov 27 '22 00:11

Goutham


2 Answers

You can use OuterClass.this:

public class Test {

    String name; // Would normally be private of course!

    public static void main(String[] args) throws Exception {
        Test t = new Test();
        t.name = "Jon";
        t.foo();
    }

    public void foo() {
        Runnable r = new Runnable() {
            public void run() {
                Test t = Test.this;
                System.out.println(t.name);
            }
        };
        r.run();
    }
}

However, if you just need to access a member in the enclosing instance, rather than getting a reference to the instance itself, you can just access it directly:

Runnable r = new Runnable() {
    public void run() {
        System.out.println(name); // Access Test.this.name
    }
};
like image 60
Jon Skeet Avatar answered May 17 '23 08:05

Jon Skeet


TreeSelectionListener is an interface, so the only parent class would be Object, which you should be able to call with super.

If you meant calling some method of the enclosing class, you can call it directly as within a method.

like image 20
Christian Strempfer Avatar answered May 17 '23 09:05

Christian Strempfer