Calling methods of “parent” component in Java

前端 未结 5 1073
庸人自扰
庸人自扰 2020-12-10 08:46

I have the following situation I think would be best to show in sample program code. I have a Java class that extends JPanel. In this class are two objects wh

5条回答
  •  隐瞒了意图╮
    2020-12-10 09:21

    To access a method on the Outer class, you need to use the syntax of Outer.this.method().

    So in this case, to access a method on the "TheTable" class from the listener, you should use TheTable.this.callMethod().

    I assume that TopPanel has added to it the TheTable. In this case, you could do something like:

    mouseClicked(...) {
       TopTable t = (TopTable)TheTable.this.getParent();
       t.MethodToFire();
    }
    

    This is a direct answer to the question, not necessarily the "best" way to do it. A type-safe composition approach would be a better long-term solution.

提交回复
热议问题