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

时间:2011-12-02 20:46:13

标签: java swing document jeditorpane javax.swing.text

我有JEditorPane持有自定义EditorKit和自定义Document(从DefaultStyledDocument派生)。

以下是JEditorPane的内容示例:


第一个段落

第二段


对于上面的例子,我得到一个文档结构,其中包含以下XML等价物:

<root>
    <section>

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

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

    </section>
</root>

请注意,上面的标记名称由Element.getName()函数确定。

我的意图是,通过自定义元素类型扩展此结构,以编辑样式文本以外的内容。

一个例子是将编辑器扩展为音乐笔记编辑器以获得这样的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>

在我看来,Style-和Paragraph-Elements是在Document.insertString()和Document.setCharacterAttributes()方法上创建的。

我的问题是,我不知道如何覆盖这些方法(或编写吊坠),以避免回到默认结构,而是使用自定义元素种类。

根本不知道这是不是正确的做法。我是否必须创建自己的Document-interface实现来创建自定义文档结构?

1 个答案:

答案 0 :(得分:1)

请参阅表创建示例。 http://java-sl.com/JEditorPaneTables.html

您可以使用相同的定义所需结构。

相关问题