create Hyper Link Label - Java Swing

Java examples for Swing:JLabel

Description

create Hyper Link Label

Demo Code


//package com.java2s;
import java.awt.Cursor;
import java.awt.Desktop;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;

public class Main {
    public static void createHiperLink(JLabel label, final String url,
            String text) {//w w w.j  a v  a2  s  . c  om
        label.setToolTipText(url);
        label.setText("<html><a href=\"\">" + text + "</a></html>");
        label.setCursor(new Cursor(Cursor.HAND_CURSOR));
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    Desktop.getDesktop().browse(new URI(url));
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } catch (URISyntaxException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}

Related Tutorials