liens cliquables en JOptionPane
j'utilise un JOptionPane pour afficher des informations sur les produits et j'ai besoin d'ajouter des liens vers des pages web.
j'ai compris que vous pouvez utiliser un JLabel contenant du html, donc j'ai inclus un lien <a href>
. Le lien apparaît bleu et souligné dans le dialogue, mais il n'est pas cliquable.
Par exemple, cela devrait aussi fonctionner:
public static void main(String[] args) throws Throwable
{
JOptionPane.showMessageDialog(null, "<html><a href="http://google.com/">a link</a></html>");
}
Comment puis-je obtenir des liens cliquables dans un JOptionPane?
Merci, Paul.
MODIFIER - par exemple la solution
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);
}
3 réponses
vous pouvez ajouter n'importe quel composant à un JOptionPane.
donc ajouter un JEditorPane qui affiche votre HTML et supporte un HyperlinkListener.
on dirait que cela a été discuté assez à fond ici comment ajouter l'hyperlien dans JLabel
la solution proposée ci-dessous ne fonctionne pas avec Nimbus Look and Feel. Nimbus remplace la couleur de fond et rend le fond blanc. La solution est de définir la couleur de fond dans le css. Vous devez également supprimer la bordure du composant. Voici une classe qui implémente une solution qui fonctionne avec Nimbus (Je n'ai pas vérifié les autres L&f):
import java.awt.Color;
import java.awt.Font;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MessageWithLink extends JEditorPane {
private static final long serialVersionUID = 1L;
public MessageWithLink(String htmlBody) {
super("text/html", "<html><body style=\"" + getStyle() + "\">" + htmlBody + "</body></html>");
addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
// Process the click event on the link (for example with java.awt.Desktop.getDesktop().browse())
System.out.println(e.getURL()+" was clicked");
}
}
});
setEditable(false);
setBorder(null);
}
static StringBuffer getStyle() {
// for copying style
JLabel label = new JLabel();
Font font = label.getFont();
Color color = label.getBackground();
// 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;");
style.append("background-color: rgb("+color.getRed()+","+color.getGreen()+","+color.getBlue()+");");
return style;
}
}
Utilisation:
JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));