Sto usando un JOptionPane per visualizzare alcune informazioni sul prodotto e ho bisogno di aggiungere alcuni collegamenti alle pagine web.link selezionabili in JOptionPane
Ho capito che è possibile utilizzare un JLabel contenente html, quindi ho incluso un collegamento <a href>
. Il collegamento si presenta blu e sottolineato nella finestra di dialogo, tuttavia non è selezionabile.
Ad esempio, questo dovrebbe funzionare anche:
public static void main(String[] args) throws Throwable
{
JOptionPane.showMessageDialog(null, "<html><a href=\"http://google.com/\">a link</a></html>");
}
Come posso ottenere link cliccabili all'interno di un JOptionPane?
Grazie, Paul.
EDIT - ad esempio soluzione
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);
}
alla soluzione postato non riesco a trovare la classe ProcessHandler. Da dove proviene? – alexandre1985