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 10 of 10

Thread: Changing Cursor Properly

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing Cursor Properly

    Ok guys pretty confused with this. What I am aiming to do is have the cursor the same in the applet the whole time! My code to set the cursor is this:
    c = new Cursor(Cursor.CROSSHAIR_CURSOR);
    this.setCursor(c);
    It is currently in the init() block of code. I don't think this is right because when I go to give focus to the applet it changes back to the normal cursor :S However sometimes when I change to a different window then go back to the applet and give it focus it will stay the same :S Always the first time I give the applet focus though it changes back to the black pointer I've even tried continously changing the cursor back to crosshaired every time my code loops (its a game) but it doesn't change it back! HELP?!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Changing Cursor Properly

    If you want help, you'll have to provide an SSCCE that we can play with. Make sure it's as small as possible while still demonstrating your problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Cursor Properly

    import java.applet.*;
    import java.awt.*;
    public class NewClass extends Applet implements Runnable{
        Cursor c;
     
        public void init(){
        c = new Cursor(Cursor.CROSSHAIR_CURSOR);
            this.setCursor(c);
        }
     
        public void run(){
     
        }
     
    }
    Last edited by JavaPF; November 4th, 2011 at 11:40 AM. Reason: Please use highlight tags

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Changing Cursor Properly

    Works for me, regardless of focus.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Cursor Properly

    I'm running macOSX. would that make a difference? Like I start it in my browser, then when I hover my mouse over it, it turns into the crosshair. but once i click on it, it turns back to a normal mouse :S

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Changing Cursor Properly

    Sure, this could be OS-dependent behavior. I'm on Windows XP, and it seems to work fine.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Cursor Properly

    Any idea on how to fix it?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Changing Cursor Properly

    Sadly, no. Java (purposely) has little control over what goes on outside of it. There are quite a few behaviors that are OS-specific (one example I can think of is how window resize events are handled- windows gives them continuously, mac waits until you release the mouse), and often there isn't much we can do about it. Is this really a deal-breaker for you? Stuff like this is pretty normal, I think.

    Do you have a website with that applet on that we could take a look at?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing Cursor Properly

    Ok i figured it out! yay! By doing some tests with the cursor I figured out that for some reason the cursor wasn't being drawn by it's type properly. It was crosshair but being drawn as a normal pointer :S The way I fixed this was changing the cursor to its default and then back to the cursor I wanted it to look like. I guess this change causes the system to update the picture it's drawing? Anyways heres the code I used...

    import java.applet.*;
    import java.awt.*;
    public class NewClass extends Applet implements Runnable{
    Cursor c,c2;

    public void init(){
    c = new Cursor(Cursor.HAND_CURSOR);
    c2 = new Cursor(Cursor.DEFAULT_CURSOR);
    this.setCursor(c2);
    this.setCursor(c);
    }

    public void run(){

    }
    public boolean mouseDown(Event e,int x,int y){
    this.setCursor(c2);
    this.setCursor(c);
    System.out.println(""+this.getCursor());
    return true;
    }
    }
    Another question is there a way I can execute code like mouseDown but for gaining focus? like focusGained or something? :S

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Changing Cursor Properly

    That is weird, but I'm glad you got it sorted out.

    And yeah, there certainly is: How to Write a Focus Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: June 27th, 2011, 11:14 AM
  2. Replies: 7
    Last Post: June 26th, 2011, 11:16 AM
  3. changing the splitpane's cursor
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 8th, 2010, 11:11 AM
  4. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 09:02 AM
  5. Replies: 6
    Last Post: August 30th, 2009, 04:31 AM

Tags for this Thread