Can I change the the mouse cursor to a different image when the mouse moves over a specific component in Java?

Simon Brown

All you need to do is add a mouse listener to your component and implement the mouseEntered and mouseExited methods to set and unset the cursor respectively. Here's an example :


myComponent.addMouseListener(new MouseAdapter() {
  public void mouseEntered(MouseEvent e) {
    myComponent.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  }

  public void mouseExited(MouseEvent e) {
    myComponent.setCursor(Cursor.getDefaultCursor());
  }
});
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact