setCursor() Not Accepting java.awt.Cursor
I'm confounded by an error I'm getting when I try to use the setCursor() method from within a JPanel.
Code :
import javax.swing.*;
import java.awt.event.*;
import java.awt.Cursor;
public class TestPanel extends JPanel {
public TestPanel() {
this.AddMouseListener();
}
public void AddMouseListener() {
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
});
}
}
When I try to compile this, I get the error:
Quote:
cannot find symbol - method SetCursor(java.awt.Cursor); maybe you meant: getCursor() or setCursor(Cursor)
Is there a Cursor class other than the java.awt.Cursor class? I can't find any trace of another Cursor in the Java API. Even then, why would they be incompatible?
Re: setCursor() Not Accepting java.awt.Cursor
The keyword 'this' always refers to the enclosing object. In the context of the inner class listener you have written, 'this' refers to the MouseListener. To refer specifically to the outer class, use the syntax MyOuterClassName.this (eg TestPanel.this.setCursor(...))
Re: setCursor() Not Accepting java.awt.Cursor
That makes sense. Thank you.
I had no idea you could reference nested objects in such a way.