So, a little bit of a "hack" (not really, but not nice either)...
Make use of a JEditorPane
...

public class TestPane extends JPanel {
public TestPane() {
JEditorPane field = new JEditorPane();
field.setContentType("text/html");
field.setText("<html><a href='https://google.com'>Google it</a></html>");
field.setEditable(false);
field.setBorder(null);
field.setOpaque(false);
setLayout(new GridBagLayout());
add(field);
}
}
You could also then use something like Hyperlink in JEditorPane to actually follow the link
Another approach might be to provide a JPopupMenu
for JLabel

public class TestPane extends JPanel {
public TestPane() {
JLabel field = new JLabel("<html><a href='https://google.com'>Google it</a></html>");
field.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(field, gbc);
add(new JTextField(20), gbc);
JPopupMenu menu = new JPopupMenu();
menu.add(new CopyAction("https://google.com"));
menu.add(new OpenAction("https://google.com"));
field.setComponentPopupMenu(menu);
}
public class CopyAction extends AbstractAction {
private String url;
public CopyAction(String url) {
super("Copy");
this.url = url;
}
@Override
public void actionPerformed(ActionEvent e) {
Toolkit tk = Toolkit.getDefaultToolkit();
Clipboard cb = tk.getSystemClipboard();
cb.setContents(new StringSelection(url), null);
}
}
public class OpenAction extends AbstractAction {
private String url;
public OpenAction(String url) {
super("Follow");
this.url = url;
}
@Override
public void actionPerformed(ActionEvent e) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Action.BROWSE)) {
try {
desktop.browse(new URL(url).toURI());
} catch (IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
}
}
}
I'd be VERY tempted to add a MouseListener
to the JLabel
and on the left mouse button click, simple follow the link, but that's me