Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Editor Kit mystery? I don't get it.

Reply
Thread Tools

Editor Kit mystery? I don't get it.

 
 
fiziwig
Guest
Posts: n/a
 
      08-17-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Training Kit vs. Resource Kit: Server 2008 Blue Olive MCITP 1 01-20-2009 05:48 PM
To change the default font of HTML editor kit without altering the edited HTML Daniel Polansky Java 0 10-11-2004 05:16 PM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
Re: How to retrieve the link's label with HTML Editor kit? Sandip Chitale Java 0 06-30-2003 02:59 PM



Advertisments