F
fiziwig
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
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