Results 1 to 4 of 4
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Making the mouse cursor go to a specific point within a JPanel

    I've used a Robot to make the mouse cursor go to a specific point on the PC's entire display. What I want is to position it right on top of something I've just drawn on the JPanel, using coordinates relative to the upper left corner of the JPanel. I'm sure this is quite straightforward but in an hour of rummaging around I could not find anything. If I knew the screen coordinates of the upper left corner of my JPanel that would be sufficient.

    Using getLocation() on the JPanel itself returns (0,0).

    Thanks,

    DL
    Last edited by Digital Larry; 03-09-2015 at 02:46 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Making the mouse cursor go to a specific point within a JPanel

    Look at the Component class. It has methods that return the component's position.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Making the mouse cursor go to a specific point within a JPanel

    Simple google for "java jpanel screen coordinates" returned me this as the first hit: java - How to get screen position of a Swing element? - Stack Overflow


    Edit:

    it will matter greatly WHEN you get the screen coordinates though; you want to fetch them after the layout work has been done. So if for example you have a call to pack() somewhere, you want to fetch the coordinates after that call and not before.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    87
    Rep Power
    0

    Default Re: Making the mouse cursor go to a specific point within a JPanel

    OK, I got it. The following method is contained within the JPanel extended class.

    Java Code:
    public void putMouseOnBlock(SpinCADBlock b) {
    		Point p = new Point();
    		p.setLocation(b.getX() + b.width/2, b.getY() + b.height/2);
    		SwingUtilities.convertPointToScreen(p, this);
    		moveMouse(p);
    	}

Similar Threads

  1. Changing the Mouse Cursor
    By CuppaCoffee in forum AWT / Swing
    Replies: 2
    Last Post: 12-22-2012, 10:41 PM
  2. Custom Cursor Mouse
    By mistercaste in forum Java 2D
    Replies: 3
    Last Post: 03-24-2010, 08:56 PM
  3. button that runs away from the mouse cursor
    By MartinBenes71 in forum New To Java
    Replies: 1
    Last Post: 01-28-2010, 08:00 AM
  4. change the mouse cursor - crosshair cursor
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 09-28-2009, 01:57 AM
  5. how to point cursor on the 1st text field in a jframe
    By VineetKumar in forum AWT / Swing
    Replies: 4
    Last Post: 04-17-2009, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •