aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need help with JEditorPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with JEditorPane" Watch "Need help with JEditorPane" New topic
Author

Need help with JEditorPane

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
I've been trying for some time to get a JEditorPane to properly display text formatted using HTML. No matter what I try, I just can't seem to get it to work. I can't find any tutorials that help either. If someone could please provide me a short example and/or a good explanation of how to do this, it would be greatly, greatly appreciated.

p.s. I want the user to be able to edit the text and apply formatting.

Thank you in advance!


Life is 10% what happens and 90% how you react to it.
Attitude is everything.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Derek Boring:
I've been trying for some time to get a JEditorPane to properly display text formatted using HTML. No matter what I try, I just can't seem to get it to work.


You really should tell the details. (Doesn't work how exactly?)

That said, this thread from last month may (or may not) be pertinent.


bitguru blog
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
I making a simple formatted text editor, such as WordPad that comes with Windows. Nothing fancy like MS Word or OpenOffice Write, just simple formatting that can be done all with HTML. It's kind of like a journal, the reason I'm using HTML instead of RTF is because I want to be able to display the text in a JLabel with the formatting later on. What doesn't work is that the JEditorPane shows the HTML tags rather than formatting the text between them. I've tried both typing in the tags myself and using something similar to txtDoc.setText("<B>This is bold.</B>"); or txtDoc.setText("<HTML><B>This is bold.</B></HTML>"); and it still just shows the tags. (even the <HTML></HTML> tags)
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Derek Boring:
What doesn't work is that the JEditorPane shows the HTML tags rather than formatting the text between them. I've tried both typing in the tags myself and using something similar to txtDoc.setText("<B>This is bold.</B>"); or txtDoc.setText("<HTML><B>This is bold.</B></HTML>"); and it still just shows the tags. (even the <HTML></HTML> tags)


It sounds like you haven't set up the JEditorPane to use the HTMLEditorKit.
As mentioned in the linked thread, the easiest way to do this is to instantiate it like this:

JEditorPane subjectPanel = new JEditorPane("text/html", "");

Replace "" with some HTML markup if you wish, or call setText(). It should interpret the HTML tags, not display them as text.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with JEditorPane