Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: setCursor() Not Accepting java.awt.Cursor

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.

    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:
    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?
    Last edited by Destro; July 14th, 2012 at 06:16 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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(...))

  3. The Following User Says Thank You to copeg For This Useful Post:

    Destro (July 14th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Replies: 2
    Last Post: January 9th, 2012, 10:03 AM
  2. Replies: 2
    Last Post: November 17th, 2011, 08:03 AM
  3. [SOLVED] problem with accepting int!
    By arvindbis in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2011, 02:10 AM
  4. changing the splitpane's cursor
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 8th, 2010, 12:11 PM
  5. Replies: 6
    Last Post: August 30th, 2009, 04:31 AM