2012-05-08 3 views
5

JEditorPane에서 클릭 가능한 링크 목록을 표시하려고합니다. 여기에 내 코드 :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>"); 


    } 
} 

I는 APPEND 부가적인 링크()을 표시하려고 때 그때의 setText() 생성자 메소드를 사용할 때, 클릭 가능한 링크를 도시되지만; 메서드는 텍스트와 함께 html 태그를 보여 주며 내 URL을 하이퍼 링크로 만들지 않습니다. 어떤 아이디어, 왜 그것이 추가와 함께 작동하지 않습니다?

답변

4

사용 중 하나는 HTMLEditorKit

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

의 방법 또는 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

고마워 Stanislav을! 나는 그것을 작동시킬 수 있었다 :) 비슷한 게시물에 대한 귀하의 의견 중 다른 하나를 발견하고 내 문제를 해결할 수 있도록 도왔습니다. – curious

관련 문제