24

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?

Abhinav
  • 1,720
  • 4
  • 21
  • 33

5 Answers5

69

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

JLabel label = new JLabel("https://stackoverflow.com");
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
Hossein Mobasher
  • 4,382
  • 5
  • 46
  • 73
2

go to the properties of the button or the label and scroll down you will find a column of cursor you can change to hand or whatever you want

chamy
  • 21
  • 1
1

As said you'd want to call the setCursor() method on the JLabel and set it to Cursor.Hand_CURSOR to further this you can also underline the text to make it an HTML look alike link if you want :):

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
import javax.swing.JLabel;

/**
 *
 * @author ludovicianul
 */
public class URLLabel extends JLabel {

    private String url;

    public URLLabel() {
        this("","");
    }

    public URLLabel(String label, String url) {
        super(label);

        this.url = url;
        setForeground(Color.BLUE.darker());
        setCursor(
                new Cursor(Cursor.HAND_CURSOR));
        addMouseListener(
                new URLOpenAdapter());
    }

    public void setURL(String url) {
        this.url = url;
    }

    //this is used to underline the text
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.blue);

        Insets insets = getInsets();

        int left = insets.left;
        if (getIcon() != null) {
            left += getIcon().getIconWidth() + getIconTextGap();
        }

        g.drawLine(left, getHeight() - 1 - insets.bottom, (int) getPreferredSize().getWidth()
                - insets.right, getHeight() - 1 - insets.bottom);
    }

    private class URLOpenAdapter extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                } catch (Throwable t) {
                    //
                }
            }
        }
    }
}

Reference:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • I feel URL feel is sufficient only by the below two line of code. `JLabel mylabel=new JLabel("Click here"); mylabel.setForeground(Color.BLUE.darker());` – Abhinav Jul 28 '12 at 03:04
  • @Abhinav And I dont disagree. I just love writing code in its pure form :P – David Kroukamp Jul 28 '12 at 14:54
0

Hossein Mobasher answer is good and mine answer is very late but i just want to add up you can also use it like this below.

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

It will also work i have tried it in Java 1.8

Ahmad
  • 770
  • 9
  • 21
0

ButtonName.SetCursor(new Cursor(12)); This one works definitely!