previous document in JEditorPane has lingering state, how to avoidthat?

R

Roy

Hi,

I am using a JEditorPane to open plain text file and html file
alternately. If I open the html file first, and then the plain text,
some punctuation marks in the plain text have changed. For example,
from ' to ', from " to " . However, if I open plain text first,
everything looks fine.

I guess the previous html document has installed something into the
document that can influence future contents.

Is there any way to avoid this? Is there any way to clear everything
about the previous document in the JEditorPane so that it is ready for
future document, which can be either html or plain text ?

Thanks a lot
Roy
 
R

Roy

Hi,

I am using a JEditorPane to open plain text file and html file
alternately. If I open the html file first, and then the plain text,
some punctuation marks in the plain text have changed. For example,
from ' to ', from " to " . However, if I open plain text first,
everything looks fine.

I guess the previous html document has installed something into the
document that can influence future contents.

Is there any way to avoid this? Is there any way to clear everything
about the previous document in the JEditorPane so that it is ready for
future document, which can be either html or plain text ?

Thanks a lot
Roy

Well, from my previous post, you probably won't be able to see the
difference between those punctuations. But they are indeed different
before and after opening the html files.
 
D

Daniele Futtorovic

Well, from my previous post, you probably won't be able to see the
difference between those punctuations. But they are indeed different
before and after opening the html files.

Could it be you're using JEditorPane#setText(String) to "open" a new
file? This could account for some weird behaviour. As the Javadoc states:
JEditorPane#setText(String)
"Sets the text of this TextComponent to the specified content, which
is expected to be in the format of the content type of this editor. For
example, if the type is set to text/html the string should be specified
in terms of HTML."

The "content type of this editor" referred to is determined by what you
first displayed in your JEditorPane.

Solution: don't use setText. Either work with Document Objects, or
change the EditorKit (I'm not sure whether the first one would suffice).

The most complete way of handling this would probably be:
- Have two types of EditorKits ready, one for handling HTML
(javax.swing.text.html.HTMLEditorKit according to the Javadoc) and one
for handling plain text (DefaultEditorKit).
- When "loading" a new file, based on its type, register the proper
EditorKit with the JEditorPane (JEditorPane#setEditorKit).
- Create an empty Document using the EditorKit just chosen's
createDefaultDocument() method
- Read the Document's content using one of the read methods of EditorKit
- Register the Document just created with the JEditorPane using
JEditorPane#setDocument.

confer:
"About Documents"
<http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#document>

"About Editor Kits":
<http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#editorkits>

"How to Use Editor Panes and Text Panes":
<http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html>

JEditorPane Javadoc:
<http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html>

DF.
 
M

Mark Space

Roy said:
I guess the previous html document has installed something into the
document that can influence future contents.

Is there any way to avoid this? Is there any way to clear everything
about the previous document in the JEditorPane so that it is ready for
future document, which can be either html or plain text ?

I guess the obvious answer would be to make a new JEditorPane each time
you switch documents or document types. If there really are tricky
internal things going on with state information, then tossing the old
object out and using a new one might be the best way to make absolutely
certain that you don't have future problems crop up.
 
D

Daniele Futtorovic

etc.

That usage of "confer" is marvelous, simply delightful. I was just
looking up that usage of the word the other day, coincidentally, in the
context of the abbreviation "cf.". Sheer poetry to see it spelled out.
Thank you.

You're most welcome.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top