473,471 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JTextPane trouble

23 New Member
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 3683
JosAH
11,448 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
@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: 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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.