简体   繁体   中英

How to create a custom Swing Document structure

I have a JEditorPane holding a custom EditorKit and a custom Document (derived from DefaultStyledDocument).

The following is an example for the content of the JEditorPane :


first paragraph

second paragraph


For the example above I get a document-structure with the following XML-equivalent:

<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.

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:

<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.

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?

See the example of tables creation. http://java-sl.com/JEditorPaneTables.html

You can use the same defining desired structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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