Domanda

this.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {

            // How do I access the parent tree from here?           
        }           
    });
È stato utile?

Soluzione

TreeSelectionListener è un'interfaccia, quindi l'unica classe padre sarebbe Object, che si dovrebbe essere in grado di chiamare con super.

Se si intende chiamare un metodo della classe che racchiude, è possibile chiamare direttamente come all'interno di un metodo.

Altri suggerimenti

È possibile utilizzare 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();
    }
}

Tuttavia, se avete solo bisogno di accedere a un membro nel caso che racchiude, piuttosto che ottenere un riferimento all'istanza stessa, si può solo accedere direttamente:

Runnable r = new Runnable() {
    public void run() {
        System.out.println(name); // Access Test.this.name
    }
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top