5

I am trying to add a hyperlink to a JPanel. I would like to make it's text blue (and underlined) and the link should be selectable (to copy some part of it). So I tried to use JLabel: yes, it allow to write something [awful] like this:

someLabel.setText("<html><font color=\"#0000ff\"><u>http://example.com</u></font></html>");

But unfortunately, JLabel doesn't allow to select any text. I also tried to use JTextField, but in opposite, it doesn't allow to use HTML/CSS in it's fields.

So, Is where exists any way to create a hyperlink (with proper indication) with basic Swing components, which will be allow to select [and copy] part of it, or should I try to use some 3rd party components? Thank you.

Oleg Kuznetsov
  • 196
  • 2
  • 6
  • 19

3 Answers3

6

You can display HTML content in a non-editable JEditorPane. It's selectable, and you can make the links functional via a HyperlinkListener:

    JEditorPane content = new JEditorPane();
    content.setContentType("text/html");
    content.setEditable(false);
    content.setText("<html><a href=\"http://stackoverflow.com\">Link</a></html>"));
    content.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    Desktop.getDesktop().browse(e.getURL().toURI());
                } catch (Exception e1) {
                    Logger.getLogger(getClass()).error(
                            "Error opening link " + e.getURL(), e1);
                }
            }
        }
    });
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2

Here how can you create a JLabel with a hyperlink ,then you can just add it to your Jpanel:

public HyperLinkLabel()  
{  
JPanel p = new JPanel();  
final String strURL = "http://www.yahoo.com";  
final JLabel label = new JLabel("<html><a href=\" " + strURL + "\"> click </a></html>");  

final JEditorPane htmlPane = new JEditorPane();  


p.add(label);  

getContentPane().add(BorderLayout.NORTH, p);  
getContentPane().add(BorderLayout.CENTER, new JScrollPane(htmlPane));  
setBounds(20,200, 500,500);  

label.addMouseListener(new MouseAdapter() {  
   public void mouseEntered(MouseEvent me) {  
      label.setCursor(new Cursor(Cursor.HAND_CURSOR));  
   }  
   public void mouseExited(MouseEvent me) {  
      label.setCursor(Cursor.getDefaultCursor());  
   }  
   public void mouseClicked(MouseEvent me)  
   {  
      System.out.println("Clicked on Label...");  
      try {  
           htmlPane.setPage(new URL(strURL));  
        }  
        catch(Exception e) {  
           System.out.println(e);  
        }  
   }  
  });  
javing
  • 12,307
  • 35
  • 138
  • 211
  • 3
    The problem with your example as it stands, is that it is not keyboard focusable. Though such functionality is easily added. 1) Set the `JLabel` focusable. 2) Add a `FocusListener` that changes the color of the text (i.e. `setForeground(Color)`) on focus gained/lost. Changing the `Color` might be a good idea in the `MouseListener` as well. – Andrew Thompson Apr 08 '11 at 22:20
0

You have to create a custom Jlabel [extend Jlabel] and write a MouseListener for the JLabel. Your mouse listener must do the job of directing the user to the link when the user clicks on the custom JLabel. The mouse event [basically the method of the MouseListener interface where you have to write the redirecting code] that you are looking for is mouseClicked.

Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
  • Is my solution incorrect? I gave the above solution of creating a custom component in the view that he will use this component in many places. Having the code in a class as a separate component facilitates reuse of the code. – Swaranga Sarma Apr 09 '11 at 08:41
  • That comment seems to have no connection with 'focusable', which is what I was referring to. (No, your solution is 'correct', but it would be better to tweak it a little.) – Andrew Thompson Apr 09 '11 at 09:37