Is it possible to create a custom hyperlink with basic Swing components in Java?

I am trying to add a hyperlink to JPanel. I would like to make the text blue (and underlined) and the link should be selected (in order to copy part of it). So I tried using JLabel: yes, it allows you to write something [terrible] like this:

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

But, unfortunately, JLabel does not allow you to select any text. I also tried using JTextField, but in contrast, it does not allow the use of HTML / CSS in fields.

So, is there any way to create a hyperlink (with the correct indication) with the basic Swing components that will allow me to select [and copy] a part of it, or should I try to use some third-party components? Thank.

+5
source share
4 answers

You can display HTML content in an inaccessible for editing JEditorPane. It is selected, and you can make the links functional with 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);
                }
            }
        }
    });
+5
source

Here is how you can 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);  
        }  
   }  
  });  
+1
source

+1

You need to create a custom Jlabel[extend Jlabel] and write MouseListenerfor Jlabel. Your mouse listener must complete the task of directing the user to the link when the user clicks on the user Jlabel. The mouse event [basically an interface method MouseListenerwhere you need to write the redirect code] that you are looking for is this mouseClicked.

0
source

All Articles