I am trying to have a clickable link with my JoptionPane message box. I got started with this link: clickable links in JOptionPane but it does not seem to work and the link is not clickable. How could I do this?
Asked
Active
Viewed 2,007 times
0
-
Make a JLabel, configure as you like, add a MouseListener to it. Use this JLabel as the "message" parameter to the JOptionPane, when mouseClicked is called, use Desktop to "browse" to the link location – MadProgrammer May 06 '15 at 10:44
1 Answers
2
The solution given by @camickr in the link that you have shared works... with a little more digging
Here is what I did
I had to set the EditorKit of the JEditorPane to be able to process "text/html" and then I made the JEditorPane non editable, then I had to handle the Hyperlink by implementing a HyperlinkListener and it worked!
Here is my code:
public class HtmlInSwing {
private static JFrame frame;
private static JPanel panel;
private static JOptionPane optionPane;
private static JEditorPane editorPane;
public static void main(String[] args) {
frame = new JFrame("Demo frame...");
panel = new JPanel();
panel.setBackground(Color.CYAN);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
optionPane = new JOptionPane();
optionPane.setSize(400, 300);
editorPane = new JEditorPane();
panel.add(optionPane, BorderLayout.CENTER);
optionPane.add(editorPane, BorderLayout.CENTER);
editorPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
editorPane.setEditable(false);
HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(frame, editorPane);
editorPane.addHyperlinkListener(hyperlinkListener);
editorPane.setText("<a href='http://www.stackoverflow.com'>Go to the stack</a>");
editorPane.setToolTipText("if you click on <b>that link you go to the stack");
frame.setVisible(true);
}
}
And here is an illustrative image:
Now here is the HyperlinkListener's implementation.
class ActivatedHyperlinkListener implements HyperlinkListener {
Frame frame;
JEditorPane editorPane;
public ActivatedHyperlinkListener(Frame frame, JEditorPane editorPane) {
this.frame = frame;
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Runnable runner = new Runnable() {
public void run() {
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
JOptionPane.showMessageDialog(frame,
"Error following link", "Invalid link",
JOptionPane.ERROR_MESSAGE);
editorPane.setDocument(doc);
}
}
};
SwingUtilities.invokeLater(runner);
}
}
}
Here is a snapshot of the result (I will leave the cosmetic details to be fixed by you, as I am simply illustrating that it works!)

alainlompo
- 4,414
- 4
- 32
- 41