How to change the mouse pointer to finger pointer in swing?

Abhinav picture Abhinav · Jul 27, 2012 · Viewed 31k times · Source

In html when we create a hyperlink and point over it ,then it automatically changes to a finger pointer.

So I was wondering can we achieve the same in java swings. Suppose I have a label on clicking which a new form pops-up.But I want that when the user points over the label it should change to finger pointer,showing that something will pop-up if its clicked.In this way we can differentiate that label with normal labels on the form i guess :).

But how to do something like this?

Answer

Hossein Mobasher picture Hossein Mobasher · Jul 27, 2012

You can set cursor of JLabel to Cursor.HAND_CURSOR using below code :

JLabel label = new JLabel("http://stackoverflow.com");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));