【问题标题】:How to create a custom Swing Document structure如何创建自定义 Swing 文档结构
【发布时间】:2012-01-11 20:13:16
【问题描述】:

我有一个 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>

在我看来,样式元素和段落元素是根据 Document.insertString() 和 Document.setCharacterAttributes() 方法创建的。

我的问题是我不知道如何重写这些方法(或编写挂件)以不返回默认结构而是使用自定义元素种类。

我什至不知道这是否是正确的方法。我是否必须创建自己的文档接口实现才能创建自定义文档结构?

【问题讨论】:

  • 我不久前试过这个,是的,你需要编写一个完整的文档结构,实现树的所有部分。这方面的文档不是很好,这需要你一段时间。
  • 可以通过派生 DefaultStyledDocument 来实现。接受的答案包含一个有趣的链接。 Document.insert(int offset, ElementSpec[] spec) 是解决方案。

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


【解决方案1】:

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

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

【讨论】:

  • 这就是解决方案。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多