我从默认根创建了文档分支的叶元素。我可以看到该元素已创建,但该元素未添加到现有结构的根,但我已将父级作为默认根。
考虑到这段代码,
public class MyDoc extends DefaultStyledDocument {
//private static final long serialVersionUID = 1L;
/**
*
*/
public Element createBranchElement(Element parent,AttributeSet a)
{
return super.createBranchElement(parent, a);
}
protected AbstractElement createDefaultRoot(){
return super.createDefaultRoot();
}
protected Element createLeafElement(Element parent,AttributeSet a, String arg) throws BadLocationException
{
return super.createLeafElement(parent, null, startOffset(arg),startOffset(arg)+1);
}
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent e, AttributeSet attr)
{
Element root = getDefaultRootElement();
Element branchroot = new BranchElement(root, null);
branchroot = createBranchElement(root,null);
String pName = branchroot.getName();
Element leafElement = new LeafElement(branchroot, null, 0,0);
try {
leafElement = createLeafElement(branchroot,null,"p");
//leafElement for a String "p" should be created
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}