2015-02-24 33 views
0

我想刪除JTextPane中的第一行並設置setContentType(「text/html」);能夠在JTextPane中使用html標籤。如何刪除JTextPane中的第一行並設置setContentType(「text/html」)

當我使用此代碼

的setContentType( 「text/html的」);

try { 
    Element root = getDocument().getDefaultRootElement(); 
    Element firstLine = root.getElement(0); 
    getDocument().remove(firstLine.getStartOffset(), firstLine.getEndOffset()); 
    } 
catch (Exception e) {e.toString();} 

我得到一個異常

javax.swing.text.BadLocationException: Invalid remove 

如果我評論的setContentType( 「text/html的」);我可以從JTextPane中刪除第一行。

回答

1

對於HTMLEditorKit Element firstLine = root.getElement(0);通常會返回<HEAD>但您需要<BODY>。瀏覽Element Tree,找到BODY並刪除BODY的第一個孩子。

更新: 該鏈接顯示了一個工具,可用於瞭解您的文檔結構。 http://java-sl.com/JEditorPaneStructureTool.html

您可以檢查您擁有哪些元素並確定應刪除哪個元素。