2012-05-08 25 views
5

Estoy tratando de mostrar una lista de enlaces clicables en JEditorPane. Aquí está mi código:cómo mostrar enlaces clicables en JEditorPane

import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.event.HyperlinkEvent; 
import javax.swing.event.HyperlinkListener; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Document; 
import javax.swing.text.Style; 
import javax.swing.text.html.HTMLEditorKit; 
import javax.swing.text.html.StyleSheet; 


public class GUI extends JFrame{ 
    JEditorPane editorpane=new JEditorPane(); 
    //this is the constructor 
    GUI(){ 
     JFrame frame=new JFrame("Frame"); 

     frame.add(editorpane); 
     JScrollPane scroll=new JScrollPane(editorpane); 

     editorpane.setContentType("text/html"); 
     editorpane.setEditable(false); 
     editorpane.setText("<html><body>Test <a href='http://www.java.net'>" 
+ "www.java.net</a></body></html>"); 
     StyleSheet css = ((HTMLEditorKit) 
     editorpane.getEditorKit()).getStyleSheet(); 
     Style style = css.getStyle("body"); 
     editorpane.addHyperlinkListener(new HyperlinkListener() { 
     public void hyperlinkUpdate(HyperlinkEvent e) { 
     if (e.getEventType() == 
     HyperlinkEvent.EventType.ACTIVATED) { 
     System.out.println("Open browser: " + e.getURL()); 
     } 
     } 
     }); 
     frame.setSize(512, 342); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(scroll); 
     frame.show(); 
    } 
    public void append(String s) { 
    try{ 
     Document doc = editorpane.getDocument(); 
     doc.insertString(doc.getLength(), "\n", null); 
     doc.insertString(doc.getLength(), s, null); 

    } 
    catch(BadLocationException exc){ 
    } 
    } 
    //main method 
    public static void main(String args[]){ 

    GUI gui=new GUI(); 
    gui.append("<html><body>Test <a href='http://www.java.net'>" 
+ "www.java.net</a></body></html>"); 


    } 
} 

Se está mostrando un enlace activo, cuando utilicé el método en el constructor setText(), pero cuando traté de mostrar enlace adicional con append(); método está mostrando las etiquetas html junto con el texto y no hace que mi url sea un hipervínculo. Alguna idea, ¿por qué no funciona con append?

Respuesta

4

El uso de cualquiera de los métodos HTMLEditorKit

public void insertHTML(HTMLDocument doc, int offset, String html, 
       int popDepth, int pushDepth, 
       HTML.Tag insertTag) 

o métodos de HTMLDocument

public void insertAfterEnd(Element elem, String htmlText) 
public void insertAfterStart(Element elem, String htmlText) 
public void insertBeforeStart(Element elem, String htmlText 
public void insertBeforeEnd(Element elem, String htmlText) 
+0

muchas gracias Stanislav !! Logré que funcionara :) Encontré otro de tus comentarios en una publicación similar y ambos me ayudaron a resolver mi problema. – curious

Cuestiones relacionadas