A custom Java mouse cursor that displays the mouse XY coordinates

Java mouse FAQ: How do I create a custom Java mouse cursor (mouse pointer)?

For my Java/Swing XY mouse position/coordinates application I finally dug into Swing and figured out how to create a Java mouse cursor that shows the X/Y position (coordinates) of the mouse cursor at any moment in time. I can't figure the right way to word that, so it may not make sense, but hopefully this picture of what I've done will help demonstrate my custom Java mouse cursor:

Java - custom mouse cursor - show X/Y coordinates/position/location

My Java custom mouse cursor to display the mouse x/y coordinates

Creating a custom Java mouse cursor like this turned out to be pretty simple, though it definitely helps to have a great book like Filthy Rich Clients at your disposal. To create my custom cursor, all I had to do was create a JComponent, and then override the paintComponent method, as shown in the class named AlsXYMouseLabelComponent below.

I think everything in my example Java source code is pretty straightforward, so I've just commented the code, and I won't add much more discussion to it. Here's the source code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

/**
 * @author Alvin Alexander, http://devdaily.com
 * 
 * A Java class to demonstrate how to create a
 * custom Java mouse cursor, in this case, a 
 * mouse cursor that shows the x/y coordinates of the
 * mouse as the mouse is moved over a Java/Swing application.
 *
 */
public class MouseCursorXYLabel extends JFrame 
{

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        displayJFrame();
      }
    });
  }

  static void displayJFrame()
  {
    // create a jframe as usual
    JFrame jFrame = new JFrame();
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setTitle("Mouse Cursor with Label");

    // set the jframe size and center it
    jFrame.setPreferredSize(new Dimension(400, 300));
    jFrame.pack();
    jFrame.setLocationRelativeTo(null);
    
    // create an instance of my custom mouse cursor component
    final AlsXYMouseLabelComponent alsXYMouseLabel = new AlsXYMouseLabelComponent();

    // add my component to the DRAG_LAYER of the layered pane (JLayeredPane)
    JLayeredPane layeredPane = jFrame.getRootPane().getLayeredPane();
    layeredPane.add(alsXYMouseLabel, JLayeredPane.DRAG_LAYER);
    alsXYMouseLabel.setBounds(0, 0, jFrame.getWidth(), jFrame.getHeight());

    // add a mouse motion listener, and update my custom mouse cursor with the x/y
    // coordinates as the user moves the mouse
    jFrame.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent me)
      {
        alsXYMouseLabel.x = me.getX();
        alsXYMouseLabel.y = me.getY();
        alsXYMouseLabel.repaint();
      }
    });

    // make the cursor a crosshair shape
    jFrame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    
    // display the jframe
    jFrame.setVisible(true);
  }
  
}

/**
 * This is the class that draws the x/y coordinates
 * near the mouse cursor/pointer.
 */
class AlsXYMouseLabelComponent extends JComponent
{
  public int x;
  public int y;
  
  public AlsXYMouseLabelComponent() {
    this.setBackground(Color.blue);
  }

  // use the xy coordinates to update the mouse cursor text/label
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    String s = x + ", " + y;
    g.setColor(Color.red);
    g.drawString(s, x, y);
  }
}

The only thing I can think to add at the moment is that although the image above doesn't show a crosshair cursor, my application actually does use a crosshair cursor. The software I used to make an image of my application changed the mouse cursor to a pointer when I took the screenshot, and I didn't notice it until just now.

Java custom mouse cursor with XY coordinates - summary

I hope this example of how to create a custom Java mouse cursor that shows the mouse XY coordinates/position is helpful. As you can see, with the paintComponent approach you can do whatever you want with a mouse cursor in Java. In this case I'm adding the XY position of the mouse, but in other cases you might add other text, change the background color of the mouse cursor to red or green to indicate whether a mouse drag/drop event is allowed, etc.