473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JTextPane trouble

23
I am an email client. I want to be able to send the emails in rich-text format, so the GUI uses a JTextPane as the Text Component in which the user's message is entered. The problem is that when I call textPane.getText() the returned string contains only HTML tags, no actual text. So my question is how do I get the HTML content from a JTextPane, including tags and text, to use in a JavaMail application? Here is the jist of what I have tried:

Expand|Select|Wrap|Line Numbers
  1. //a lot of GUI code
  2.  
  3. JTextPane textPane = new JTextPane();
  4. textPane.setEditorKit( new HTMLEditorKit() );
  5. textPane.setDocument( new HTMLDocument() );
  6.  
  7. //more GUI code
  8.  
  9. String message = textPane.getText();
  10.  
  11. //then code to send the email
  12.  
Having entered some arbitrarily formatted text and calling System.out.println(message) the following output is produced. (The output of course varies with the random non-sense I enter into the JTextPane whilst testing, but it is always only tags)

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.  
  4.   </head>
  5.   <body>
  6.     <p style="margin-top: 0">
  7.       <font face="Dialog" size="3"><Dialog>
  8.       <u><Dialog>
  9.       </u><b><Dialog>
  10.       </b></font><b><font face="Dialog" size="7"><Dialog>
  11. </font></b>    </p>
  12.   </body>
  13. </html>
  14.  
Mar 8 '09 #1
5 3677
JosAH
11,448 Expert 8TB
What happens when you ask your HTMLEditorKit to write its entire content? Read the API docs for the details of its write() method. For the actual Writer you can use a StringWriter so you can get the content as a String later.

kind regards,

Jos
Mar 8 '09 #2
HxRLxY
23
I have tried that, with the same result. I combed the API and thought this would work, but it does the same thing - prints only the tags.

Expand|Select|Wrap|Line Numbers
  1. HTMLEditorKit kit = (HTMLEditorKit)textPane.getEditorKit();
  2. HTMLDocument doc = (HTMLDocument)textPane.getDocument();
  3. StringWriter writer = new StringWriter();
  4.  
  5. try{
  6. kit.write( writer, doc, 0, doc.getLength() );
  7. System.out.println( writer.toString() );
  8. }
  9. catch( IOException ie ){
  10. //code to handle exception 
  11. }
  12.  
Mar 8 '09 #3
JosAH
11,448 Expert 8TB
I have a question for you: can you post a (preferably short) example that shows the behaviour you described, so that I can play with it? The API doesn't give me an answer and I find it a bit strange that there would be no way to get the original html back (I don't really care about comments in the text).

kind regards,

Jos
Mar 8 '09 #4
HxRLxY
23
Well Jos, in the process of writing a small example for you I discovered the problem. For some reason setting more than one attribute at a time with SimpleAttributeSet causes the JTextPane to get confused and lose the text. So I modified the code to change only one attribute at a time, and voila. Thanks for the help

-Harley
Mar 8 '09 #5
JosAH
11,448 Expert 8TB
@HxRLxY
You're welcome of course; what just happened to you is typical when you write an SSCCE, i.e. the mistake bubbles up to the surface and you're able to solve your problems yourself ;-)

kind regards,

Jos
Mar 9 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tony Burrows | last post by:
I have a JTextPane with several styles. The style data is loaded from/ saved to a file. I can modify the styles at run-time and the new styles are saved to disk. The next time the app is run...
1
by: Jim Bancroft | last post by:
Hi everyone, I'm running into a problem with my ASP.Net application. I've just created a new aspx page which uses some new components of mine that inherit from ServicedComponent and are...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
0
by: clase | last post by:
Hi everyone, I need your help! :) I need to build a widget that manages (read, write, edit) XML files. My idea is to build this new component on JTextPane; it allows to use RTM and HTML files so...
0
by: Grzegorz Kaczor | last post by:
Hello, I have a little problem that causes a lot of trouble to my application :). I've got a JTextPane (but I can change it to anything that can edit a StyledDocument if it will help) and...
2
by: JLupear | last post by:
I am having trouble with my code again, I had prepared a question and the code to upload, however I am having trouble posting it, are there limits to the amount of lines you can post? I split it...
1
by: linksterman | last post by:
i have searched and searched to find the answer to this problem, but alas, it eluded me and i had to turn in my project without autoscrolling (which sucks since it is a chat client) i had to use...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
2
hsriat
by: hsriat | last post by:
Can I change the DefaultStyledDocument of a JTextPane dynamically? What I want to implement is like a document selector. Selecting an item from the list changes the text inside the JTextPane to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.