• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with JEditorPane

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic