1

Is there a way to set ac.getPage() as a hyperlink to open in broswer when clicked on? ac.getPage() returns a string which is an actual fact a url of a persons blog. I tried attaching the following java.awt.Desktop.getDesktop().browse(java.net.URI.create(ac.getPage()) and error says void is not allowed? How can i correct this?

pageLabel.setText("Page:    " + ac.getPage());
Hoody
  • 2,942
  • 5
  • 28
  • 32
  • seems similar with this question http://stackoverflow.com/questions/527719/how-to-add-hyperlink-in-jlabel – Netorica Dec 31 '12 at 11:34
  • @JoopEggen yes i did that – Hoody Dec 31 '12 at 11:46
  • @Mahan that seems too long and i would like to be able to keep the text "Page" too – Hoody Dec 31 '12 at 11:50
  • 1) Alternately use a `JTextField` for the link component as shown in [this answer](http://stackoverflow.com/a/13871898/418556). A text field is an accessible component. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 31 '12 at 11:50
  • That "void is not allowed" seems to be caused by the `void browse(URI)`. Do you assign it? Is a semicolon `;` missing before that code? `browse` opens the system browser with an URI. – Joop Eggen Dec 31 '12 at 11:57
  • @JoopEggen tried the following http://www.youtube.com/watch?v=bLek4Ycb7p8&list=PLB04B4E5D9B58C13D&index=76 if there is a way for Jlabel to work with mouselisterner should do it? – Hoody Dec 31 '12 at 12:01

1 Answers1

3

You could use a JTextPane instead of a JLabel,

JTextPane pageLabel = new JTextPane();
pageLabel.setEditable(false);
pageLabel.setText("<html>Page: <a href='http://eo.wikipedia.org/'>vikipedio</a>"):
pageLabel.addHyperLinkListener(new HyperLinkListener() {
    @Override
    public hyperlinkUpdate(HyperlinkEvent event) {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            String url = event.getURL().toString();
            Desktop.getDesktop().browse(URI.create(url));
        }
    }
});
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138