From: Lew on 16 Mar 2010 13:47 Sam Takoy wrote: >> 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? Thomas Richter wrote: > 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. -- Lew
From: Joshua Cranmer on 16 Mar 2010 13:48 On 03/16/2010 11:12 AM, Sam Takoy wrote: > 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. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: Roedy Green on 17 Mar 2010 02:05 On Tue, 16 Mar 2010 11:12:49 -0400, Sam Takoy <samtakoy(a)yahoo.com> wrote, quoted or indirectly quoted someone who said : > 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.
First
|
Prev
|
Pages: 1 2 Prev: Cross Platform/Browser way to send PNG file to Webserver Next: JLabel.setText |