简体   繁体   English

如何创建自定义的Swing文档结构

[英]How to create a custom Swing Document structure

I have a JEditorPane holding a custom EditorKit and a custom Document (derived from DefaultStyledDocument). 我有一个JEditorPane包含一个自定义EditorKit和一个自定义Document (从DefaultStyledDocument派生)。

The following is an example for the content of the JEditorPane : 以下是JEditorPane内容的示例:


first paragraph 第一段

second paragraph 第二段


For the example above I get a document-structure with the following XML-equivalent: 对于上面的示例,我得到一个具有以下等效于XML的文档结构:

<root>
    <section>

        <paragraph>
            <content>first</content>
            <content bold="true">paragraph</content>
        </paragraph>

        <paragraph>
            <content>second paragraph</content>
            <content>\n</content>
        </paragraph>

    </section>
</root>

Note that the tag names above are determined by the Element.getName() function. 请注意,上面的标签名称由Element.getName()函数确定。

My intention is, to extend this structure by custom element types to edit content other than styled text. 我的意图是通过自定义元素类型扩展此结构以编辑除样式文本之外的内容。

An example would be extending the editor to be a music-note editor to get an XML-structure like this: 一个示例是将编辑器扩展为音乐注释编辑器,以获得如下所示的XML结构:

<root>
    <section>

        <paragraph>
            <content>first</content>
            <content bold="true">paragraph</content>
        </paragraph>

        <musicnotes>
            <bar>
                <note>C</note>
                <note>D</note>
                <note>E</note>
            </bar>
        </musicnotes>

    </section>
</root>

As I see it, the Style- and Paragraph-Elements are created upon Document.insertString() and Document.setCharacterAttributes() methods. 如我所见,样式元素和段落元素是根据Document.insertString()和Document.setCharacterAttributes()方法创建的。

My problem is that I have no clue how to override these methods (or write pendants) to not to go back to the default structure but to use custom element kinds. 我的问题是我不知道如何重写这些方法(或编写吊坠),以不回到默认结构而是使用自定义元素种类。

At all I don't even know if this is the correct approach. 我什至不知道这是否是正确的方法。 Do I have to create my very own Implementation of the Document-interface to create a custom document structure? 我是否必须创建自己的Document-interface实现来创建自定义文档结构?

See the example of tables creation. 请参阅表创建示例。 http://java-sl.com/JEditorPaneTables.html http://java-sl.com/JEditorPaneTables.html

You can use the same defining desired structure. 您可以使用相同的定义所需结构。

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

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