Your Computer Science Resource

Java JLabel Hyperlink

Have you ever needed a hyperlink in your project? One convenient place for this is in the “About” dialog with a link to your website or e-mail. Unfortunately, there isn’t a JHyperlink class in the standard library, but we can make our own, right? Let’s see how to do it!

The following class, JHyperlink extends JLabel and implements MouseListener, so it has all of the properties of a JLabel (which lets us set the text, tooltip, foreground color, etc.) as well as the ability to detect if the mouse was pressed. Simply include this class in your project and use it like so:

Usage

JHyperlink link = new JHyperlink("Google", "http://www.google.com");

As you can see, it takes two parameters. The first one is the label text, and the second is the URI (as a String). You can add other constructors to take a URL or URI, in addition to the String, but I figured the String was easiest and most used. Also, remember to add the component to your container (e.g. a JPanel), because it won’t magically show up in your GUI by itself :)

Code

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;

public class JHyperlink extends JLabel implements MouseListener {

	private static final long serialVersionUID = 1L;
	private URI uri = null;

	/**
	 * Constructor
	 *
	 * @param String text
	 * @param String uri
	 */

	public JHyperlink( String text, String uri ) {

		this.setText( "<html><u>" + text + "</u></html>" );

		try {

			this.uri = new URI( uri );

		} catch (URISyntaxException e) {

			e.printStackTrace();

		}

		initialize();

	} // end constructor

	/**
	 * This method returns the URI
	 *
	 * @return uri
	 */

	public URI getURI() {

		return uri;

	} // end method getURI

	/**
	 * This method initializes the
	 * link.
	 */

	private void initialize() {

		this.setForeground( Color.BLUE );
		this.setOpaque( false );
		this.setToolTipText( getURI().toString() );
		this.addMouseListener( this );

	} // end method initialize

	@Override
	public void mouseClicked(MouseEvent e) {

		if ( e.getClickCount() > 0 ) {

			if ( Desktop.isDesktopSupported() ) {

				Desktop desktop = Desktop.getDesktop();

				try {

					desktop.browse( getURI() );

				} catch ( IOException ex ) {

					ex.printStackTrace();

				}

			}

		}

	} // end method mouseClicked

	@Override
	public void mouseEntered(MouseEvent arg0) {

		this.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );

	} // end method mouseEntered

	@Override
	public void mouseExited(MouseEvent arg0) {

		this.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );

	} // end method mouseExited

	@Override
	public void mousePressed(MouseEvent arg0) {

		this.setForeground( Color.RED );
		this.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) );

	} // end method mousePressed

	@Override
	public void mouseReleased(MouseEvent arg0) {

		this.setForeground( Color.BLUE );
		this.setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );

	} // end method mouseReleased

} // end class JHyperlink

Example Usage

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class HyperlinkTest {

	public static void main( String[] args ) {

		JFrame frame = new JFrame("Hyperlink Test");
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.setSize(300,200);

		Container container = frame.getContentPane();
		container.setLayout( new FlowLayout() );

		JHyperlink google = new JHyperlink( "Google", "http://www.google.com");
		container.add( google );

		frame.setVisible( true );

	}

}

Enjoy!

Tags:

Leave a Reply