[英]How to remove first line in JTextPane and setting setContentType(“text/html”)
I want to remove the first line in JTextPane and setting setContentType("text/html"); 我想删除JTextPane中的第一行并设置setContentType(“ text / html”); to be able to use html tags in JTextPane. 才能在JTextPane中使用html标签。
When I use this code 当我使用此代码
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();}
I get an exception 我有一个例外
javax.swing.text.BadLocationException: Invalid remove
if I commented setContentType("text/html"); 如果我评论了setContentType(“ text / html”); I can be able to remove the first line from JTextPane. 我可以从JTextPane删除第一行。
For the HTMLEditorKit the Element firstLine = root.getElement(0);
对于HTMLEditorKit, Element firstLine = root.getElement(0);
normally returns <HEAD>
but you need <BODY>
. 通常返回<HEAD>
但您需要<BODY>
。 Go through the Element Tree, find the BODY and remove the first child of BODY. 遍历元素树,找到BODY并删除BODY的第一个子代。
UPDATE: The link shows a tool you can use to understand your Document structure. 更新:链接显示了可用于了解文档结构的工具。 http://java-sl.com/JEditorPaneStructureTool.html http://java-sl.com/JEditorPaneStructureTool.html
You can check which elements you have and figure out which exactly element should be removed. 您可以检查拥有的元素,并确定应该删除的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.