简体   繁体   English

如何为现有文档结构创建文档的叶元素

[英]How to create a leaf element of a document for an existing document structure

I have created a leaf element of a branch of a document from the default root. 我已经从默认根目录创建了文档分支的叶子元素。 I can see that the element is created but the element is not added to the root of the existing structure but I have given the parent as the default root . 我可以看到该元素已创建,但未将其添加到现有结构的根中,但我已将父级指定为默认根。

Considering this code, 考虑这段代码,

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();
       }

    }
}      

See http://java-sl.com/JEditorPaneTables.html how tables are inserted. 请参见http://java-sl.com/JEditorPaneTables.html如何插入表。

See protected void insertTable() method. 请参见protected void insertTable()方法。 Use the same approach to create your own specs and change this way the document structure. 使用相同的方法来创建自己的规范并以此方式更改文档结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM