Editor Kit mystery? I don't get it.

Discussion in 'Java' started by fiziwig, Aug 17, 2006.

  1. fiziwig

    fiziwig Guest

    I build a JTextPane which is editable, save it to a file, and load it
    back after which it's editing behavior is completely whacked out.

    The text region is originally built like this:

    ...
    textRegion = new JTextPane();
    StyledDocument doc = textRegion.getStyledDocument();
    MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setForeground(attr, Color.black);
    if ( text!=null ) {
    try {
    doc.insertString(doc.getLength(), text, attr);
    }
    catch (BadLocationException ble) {
    System.err.println("Couldn't insert initial text into
    text pane.");
    }
    }
    ...


    After which I can edit the text and change font, size, color, bold, and
    italic.

    Then I save the contents as a string put into a larger XML file like
    this:


    public String getXmlText() {

    // translate content and all its embedded style attributes into
    XML
    // Using class: MinimalHTMLWriter

    CharArrayWriter writer = null;
    try {
    writer = new CharArrayWriter();
    MinimalHTMLWriter htmlWriter = new
    MinimalHTMLWriter(writer, (StyledDocument)textRegion.getDocument());
    htmlWriter.write();
    }
    catch (IOException ex) {
    System.out.println("Save error");
    }
    catch (BadLocationException ex) {
    System.out.println("HTML File Corrupt");
    }
    finally {
    if (writer != null) {
    writer.close();
    }
    }
    return writer.toString();
    }


    Then I reload the contents from HTML String in the saved XML file like
    this:

    public void setXmlText( String text ) {
    // Translate HTML in "text" into styled document
    System.out.println(text);
    CharArrayReader reader = new CharArrayReader(
    text.toCharArray() );
    textRegion.setContentType("text/html");
    textRegion.setText(text);
    }


    After which the editing of the text in the JTextPane is completely
    messed up.
    For example, I type the text "16 point", highlight it and set it to 16
    point
    font and what ends up in the saved document looks like this:


    <p>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    1
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    6
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">

    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    p
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    o
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    i
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    n
    </span>
    <span style="color: #000000; font-size: 18pt; font-family:
    Dialog">
    t
    </span>

    </p>


    Not only the wrong font size, but <P> tages inserted between every
    single character I type! Why is this behving so strangely?

    Thanks for any insights.

    --gary
    fiziwig, Aug 17, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandip Chitale
    Replies:
    0
    Views:
    1,270
    Sandip Chitale
    Jun 30, 2003
  2. Stylus Studio
    Replies:
    0
    Views:
    656
    Stylus Studio
    Aug 3, 2004
  3. Daniel Polansky
    Replies:
    0
    Views:
    739
    Daniel Polansky
    Oct 11, 2004
  4. Teffy
    Replies:
    3
    Views:
    518
    Sid Ismail
    Nov 10, 2003
  5. Radu
    Replies:
    1
    Views:
    1,375
    Misbah Arefin
    Feb 8, 2008
Loading...

Share This Page