1 package net.sf.openrocket.gui.components;
4 import java.awt.Cursor;
5 import java.awt.Desktop;
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.awt.font.TextAttribute;
9 import java.io.IOException;
11 import java.net.URISyntaxException;
12 import java.util.HashMap;
16 * A label of a URL that is clickable. Clicking the URL will launch the URL in
17 * the default browser if the Desktop class is supported.
19 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public class URLLabel extends SelectableLabel {
24 * Create a label showing the url it will direct to.
28 public URLLabel(String url) {
33 * Create a label with separate URL and label.
35 * @param url the URL clicking will open.
36 * @param label the label.
38 public URLLabel(final String url, String label) {
43 if (Desktop.isDesktopSupported()) {
45 // Blue, underlined font
46 Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
47 map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
48 this.setFont(this.getFont().deriveFont(map));
49 this.setForeground(Color.BLUE);
51 this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
54 this.addMouseListener(new MouseAdapter() {
56 public void mouseClicked(MouseEvent e) {
57 Desktop d = Desktop.getDesktop();
59 d.browse(new URI(url));
60 } catch (URISyntaxException e1) {
61 throw new RuntimeException("BUG: Illegal URL: " + url, e1);
62 } catch (IOException e1) {
63 System.err.println("Unable to launch browser:");