2011-12-01 8 views
20

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); 
} 
+4

alla soluzione postato non riesco a trovare la classe ProcessHandler. Da dove proviene? – alexandre1985

risposta

15

è possibile aggiungere qualsiasi componente a un JOptionPane.

Quindi aggiungere un JEditorPane che visualizza il codice HTML e supporta HyperlinkListener.

+1

sì questa è di gran lunga la soluzione più semplice che ho trovato. non è difficile e pubblicherò la mia soluzione ad esempio nella mia domanda per gli altri. – pstanton

+0

aw maaaan! l'ho postato come commento sull'altra risposta! –

5

Sembra che è stato discusso in modo abbastanza esauriente qui How to add hyperlink in JLabel

+0

showmessagedialog accetta un oggetto e se è un controllo lo inserirà semplicemente nella finestra di dialogo, in modo da poter passare la jlabel anziché il testo stesso. oppure potresti anche usare un jeditorpane di sola lettura che ha un sacco di contenuti HTML e link invece di una sola etichetta. –

3

La soluzione proposta sotto la risposta non funziona con Nimbus Look and Feel. Nimbus sostituisce il colore di sfondo e rende lo sfondo bianco. La soluzione è impostare il colore di sfondo nel css. È inoltre necessario rimuovere il bordo del componente. Ecco una classe che implementa una soluzione che funziona con Nimbus (non ho controllato altri 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; 
    } 
} 

Usage:

JOptionPane.showMessageDialog(null, new MessageWithLink("Here is a link on <a href=\"http://www.google.com\">http://www.google.com</a>"));