2011-06-11 3 views
4

스윙 텍스트 구성 요소를 실험하려고하는데이 코드에서는 JTextPane에 매우 간단한 웹 페이지를 표시하려고합니다. 나는 HTMLDocumentgetText을 호출하면 반환되는 String을 출력 할 때 웹 페이지를 읽고 JTextPane의 문서에 넣을 수 있지만 JTextPane에 아무것도 표시되지 않습니다. 나는 기본적인 것을 놓치고있는 것처럼 느낀다. 미리 감사드립니다.HTML 페이지가 Java Swing JTextPane에 표시됩니다.

내 SSCCE :

import java.awt.*; 
import java.io.IOException; 
import java.net.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.html.*; 

@SuppressWarnings("serial") 
public class TestStyledDoc2 extends JPanel { 
    public static final String GETTY_FILE = "http://www.d.umn.edu/~rmaclin/" + 
     "gettysburg-address.html"; 

    private HTMLEditorKit htmlKit = new HTMLEditorKit(); 
    private HTMLDocument htmlDocument = (HTMLDocument) htmlKit.createDefaultDocument(); 
    private JTextPane htmlPane = new JTextPane(htmlDocument); 

    public TestStyledDoc2() { 
     JScrollPane scrollPane1 = new JScrollPane(htmlPane); 
     try { 
     htmlPane.setEditorKit(htmlKit); 
     URL gettyUrl = new URL(GETTY_FILE); 
     htmlKit.read(gettyUrl.openStream(), htmlDocument, 0); 
     System.out.println(htmlDocument.getText(0, htmlDocument.getLength())); 
     } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } catch (BadLocationException e) { 
     e.printStackTrace(); 
     } 

     scrollPane1.getViewport().setPreferredSize(new Dimension(400, 400)); 

     setLayout(new BorderLayout()); 
     add(scrollPane1, BorderLayout.CENTER); 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("TestStyledDoc"); 
     frame.getContentPane().add(new TestStyledDoc2()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 
+1

이 JTextPane가보십시오. setPage (URL)를 사용하여 페이지를 렌더링 할 수 있는지 확인하십시오. –

답변

7

setEditorKit()의 통화가 처음 할당 된 문서를 제거하고 새 것으로 교체합니다. 바로 뒤에 다른 줄을 추가하여 올바른 문서를 복원하십시오.

htmlPane.setEditorKit(htmlKit); 
htmlPane.setDocument(htmlDocument); 

또는 textpane 당신은 사용되는 실제 에디터 킷 또는 문서를 알 필요가 없습니다

htmlPane.setEditorKit(htmlKit); 
htmlDocument = (HTMLDocument) htmlPane.getDocument(); 
+1

답장을 보내 주셔서 감사합니다. 당신에게 명성! –

5

에서 문서를 reget :

import java.awt.*; 
import java.io.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class EditorPaneLoad extends JFrame 
{ 
    public EditorPaneLoad() 
     throws Exception 
    { 
     FileReader reader = new FileReader("a.html"); 
//  JEditorPane editor = new JEditorPane(); 
     JTextPane editor = new JTextPane(); 
     editor.setContentType("text/html"); 
     editor.setEditable(false); 
     editor.read(reader, null); 
     System.out.println(editor.getText()); 
     System.out.println("\n------------\n"); 
     Document doc = editor.getDocument(); 
     System.out.println(doc.getText(0, doc.getLength())); 
     JScrollPane scrollPane = new JScrollPane(editor); 
     scrollPane.setPreferredSize(new Dimension(300, 200)); 
     getContentPane().add(scrollPane); 
    } 

    public static void main(String[] args) 
     throws Exception 
    { 
     EditorPaneLoad frame = new EditorPaneLoad(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
}