How to change cursor type

This question is related to the previous post. How to save and read a file

alt text http://freeimagehosting.net/image.php?dc73c3bb33.jpg

How can I change the cursor to "Hand" only when the mouse points to a grid that is not Null (contained images)?

While the cursor turns into a "Hand" along the entire grid (zero or not empty).

public GUI() { .... JPanel pDraw = new JPanel(); .... for(Component component: pDraw.getComponents()){ JLabel lbl = (JLabel)component; //add mouse listener to grid box which contained image if (lbl.getIcon() != null) lbl.addMouseListener(this); } public void mouseEntered(MouseEvent e) { Cursor cursor = Cursor.getDefaultCursor(); //change cursor appearance to HAND_CURSOR when the mouse pointed on images cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); setCursor(cursor); } 
+4
source share
2 answers

This should have the desired effect:

 public GUI() { // class attributes protected Component entered = null; protected Border defaultB = BorderFactory...; protected Border highlighted = BorderFactory...; .... JPanel pDraw = new JPanel(); .... for(Component component: pDraw.getComponents()){ JLabel lbl = (JLabel)component; //add mouse listener to grid box which contained image if (lbl.getIcon() != null) lbl.addMouseListener(this); } public void mouseEntered(MouseEvent e) { if (!(e.getSource() instanceof Component)) return; exit(); enter((Component)e.getSource()); } public void mouseExited(MouseEvent e) { exit(); } public void enter(Component c) { //change cursor appearance to HAND_CURSOR when the mouse pointed on images Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); setCursor(cursor); c.setBorder(highlighted); entered = c; } public void exit() { Cursor cursor = Cursor.getDefaultCursor(); setCursor(cursor); if (entered != null) { entered.setBorder(defaultB); entered = null; } } 

Edited post for new material in the comment. BorderFactory javadoc: http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html . Edit 2: fixed a small issue.

+5
source

Here is one way to change the cursor in a specific column in JTable:

 if(tblExamHistoryAll.columnAtPoint(evt.getPoint()) == 5) { setCursor(Cursor.HAND_CURSOR); } else { setCursor(0); } 
+3
source

Source: https://habr.com/ru/post/1305397/


All Articles