From: Sam Takoy on
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();
}
}

}
From: Thomas Richter on
Sam Takoy wrote:
> 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
From: Sam Takoy on
Thomas Richter wrote:
> Sam Takoy wrote:
>> 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

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.
From: Sam Takoy on
Stefan Ram wrote:
> Sam Takoy <samtakoy(a)yahoo.com> writes:
>> JPanel.this.revalidate();
>
> This (no pun intended) should be:
>
> MyClass.this.revalidate();
>
> .
>

Yup - thank you!
From: Alessio Stalla on
On Mar 16, 4:23 pm, Sam Takoy <samta...(a)yahoo.com> wrote:
> Thomas Richter wrote:
> > Sam Takoy wrote:
> >> 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
>
> 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