-1

I am using the code bellow to show a message dialog:

String msg = "<html>You need to download it from here: <br><b> ttp://chromedriver.storage.googleapis.com/index.html?path=2.20/ </b <br></html>";
JLabel label = new JLabel(msg);
label.setFont(new java.awt.Font("serif", java.awt.Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, label);

but users cannot copy and paste the link,

LoveJavaTwo
  • 215
  • 3
  • 17

1 Answers1

2

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

Make use of a JEditorPane...

enter image description here

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

Copy link

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366