Calling this from the built-in Java ActionListener

Suppose I have this:

class external {
    JFrame myFrame;
    ...

    class internal implements ActionListener {
        public void actionPerformed(ActionEvent e) {
             ...
             myFrame.setContentPane(this.createContentPane());
        }
    }
    ...
}

createContentPanereturns the container. Now, if I were to do this code outside ActionListener, it would work because I would have access to it. But inside this is not. I have access to myFramethat will be updated with the contents of the method, but this is not enough to do what I want, unless I can get it from it.

I also need information from other instance variables to use createContentPane(), so I'm not sure I can do this static.

+5
source share
4 answers

You can:

myFrame.setContentPane(createContentPane());

or

myFrame.setContentPane(external.this.createContentPane());

, Java- . , , , , , - .

, :

class External {
    JFrame myFrame;
    ...

        class Internal implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                ...
                myFrame.setContentPane(createContentPane());
               //Or myFrame.setContentPane(External.this.createContentPane());
            }
        }
    ...
 }

Java Code

+9

external.this , , ...

+2

, , . "this" (, ), :

someMethod(External.this);

, "this". , :

myFrame.setContentPane(createContentPane());

myFrame.setContentPane(External.this.createContentPane());

, myFrame .

+1

JFrame ,

class External extends JFrame {
.....
.....
}
0
source

All Articles