"this" pointer in a sublcass

S

Sam Takoy

Hi,

The following code doesn't compile with the error:

"No enclosing instance of the type JPanel is accessible in scope"

but isn't my class a JPanel?

Many thanks in advance!

Sam


import java.awt.event.*;
import javax.swing.JPanel;

public class MyClass extends JPanel {

public class MyMouseLisetener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
JPanel.this.revalidate();
}
}

}
 
T

Thomas Richter

Sam said:
Hi,

The following code doesn't compile with the error:
>
import java.awt.event.*;
import javax.swing.JPanel;

public class MyClass extends JPanel {

public class MyMouseLisetener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
JPanel.this.revalidate();
}
}

} >
"No enclosing instance of the type JPanel is accessible in scope"

but isn't my class a JPanel?

No, obviously not - it is a MouseAdapter. "this" always refers to the
class the member function is part of.

So long,
Thomas
 
S

Sam Takoy

Thomas said:
No, obviously not - it is a MouseAdapter. "this" always refers to the
class the member function is part of.

So long,
Thomas

Are you guessing or giving an educated answer? My impression (perhaps
false) was that the device I was attempting to use was designed for the
specific purpose of going up to the enclosing class.
 
A

Alessio Stalla

Are you guessing or giving an educated answer? My impression (perhaps
false) was that the device I was attempting to use was designed for the
specific purpose of going up to the enclosing class.

You have to use MyClass.this, not JPanel.this. The enclosing class is
not JPanel, it's MyClass.

Alessio
 
L

Lew

Thomas said:
No, obviously not - it is a MouseAdapter. "this" always refers to the
class the member function is part of.

That is incorrect information. The OP is attempting to use "outer
this", that is, 'this' for the enclosing class. So the 'this' in
question is most emphatically not a 'MouseAdapter', it's a 'MyClass',
as others have already pointed out.
 
J

Joshua Cranmer

but isn't my class a JPanel?

Your class is a subclass of JPanel, but the class JPanel does not
enclose the inner class.

The Name.this syntax requires you to specify the actual class enclosing
it, not a supertype of an enclosing class instance.
 
R

Roedy Green

JPanel.this.revalidate();

that syntax is used with you are an inner class and you are trying to
get at a method in an outer class. In this case, you want the current
class, so just use this.revalidate() or even shorter revalidate().
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top