Estoy usando JOptionPane para mostrar cierta información del producto y necesito agregar algunos enlaces a páginas web.enlaces clicables en JOptionPane
He descubierto que puede usar un JLabel que contenga html, así que he incluido un enlace <a href>
. El enlace aparece azul y subrayado en el cuadro de diálogo, sin embargo, no se puede hacer clic.
Por ejemplo, esto también debería funcionar:
public static void main(String[] args) throws Throwable
{
JOptionPane.showMessageDialog(null, "<html><a href=\"http://google.com/\">a link</a></html>");
}
¿Cómo consigo hacer clic en enlaces dentro de un JOptionPane?
Gracias, Paul.
EDITAR - por ejemplo, solución
public static void main(String[] args) throws Throwable
{
// for copying style
JLabel label = new JLabel();
Font font = label.getFont();
// create some css from the label's font
StringBuffer style = new StringBuffer("font-family:" + font.getFamily() + ";");
style.append("font-weight:" + (font.isBold() ? "bold" : "normal") + ";");
style.append("font-size:" + font.getSize() + "pt;");
// html content
JEditorPane ep = new JEditorPane("text/html", "<html><body style=\"" + style + "\">" //
+ "some text, and <a href=\"http://google.com/\">a link</a>" //
+ "</body></html>");
// handle link events
ep.addHyperlinkListener(new HyperlinkListener()
{
@Override
public void hyperlinkUpdate(HyperlinkEvent e)
{
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
ProcessHandler.launchUrl(e.getURL().toString()); // roll your own link launcher or use Desktop if J6+
}
});
ep.setEditable(false);
ep.setBackground(label.getBackground());
// show
JOptionPane.showMessageDialog(null, ep);
}
en la solución publicadas no puedo encontrar la clase ProcessHandler. ¿De dónde viene? – alexandre1985