Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this using a <code>JLabel</code>, but an alternative would be to style a <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html" rel="noreferrer"><code>JButton</code></a>. That way, you don't have to worry about <a href="http://en.wikipedia.org/wiki/Computer_accessibility" rel="noreferrer">accessibility</a> and can just fire events using an <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html" rel="noreferrer"><code>ActionListener</code></a>.</p> <pre><code> public static void main(String[] args) throws URISyntaxException { final URI uri = new URI("http://java.sun.com"); class OpenUrlAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { open(uri); } } JFrame frame = new JFrame("Links"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(100, 400); Container container = frame.getContentPane(); container.setLayout(new GridBagLayout()); JButton button = new JButton(); button.setText("&lt;HTML&gt;Click the &lt;FONT color=\"#000099\"&gt;&lt;U&gt;link&lt;/U&gt;&lt;/FONT&gt;" + " to go to the Java website.&lt;/HTML&gt;"); button.setHorizontalAlignment(SwingConstants.LEFT); button.setBorderPainted(false); button.setOpaque(false); button.setBackground(Color.WHITE); button.setToolTipText(uri.toString()); button.addActionListener(new OpenUrlAction()); container.add(button); frame.setVisible(true); } private static void open(URI uri) { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(uri); } catch (IOException e) { /* TODO: error handling */ } } else { /* TODO: error handling */ } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload