• Post Reply Bookmark Topic Watch Topic
  • New Topic

HTML from resource on classpath  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't do Swing/AWT much, so for all I know this could be trivial or it could be impossible. I hope someone can enlighten me.

I would like to display some rendered HTML in a Java Panel (*). The HTML will be in a file that I have built into one of the Jars on the classpath.

I do not have the name or location of the Jar available to me and I cannot refer to the Jar directly, so jar: URLs are out, I think. But the file resource would be easily opened via ClassLoader.getResourceAsStream().

(*) If anyone's interested, it's actually a CustomCodePanel from InstallAnywhere.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please elaborate what your question exactly is?
Is it how to render HTML in a java component or is it how to load the HTML from the jar? I am a bit confused here
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's both of those things.

I have seen, for instance, that JTextPane has setPage(URL), and it is my understanding that this will render HTML. But I don't know what, if any, kind of URL I could use to find my resource.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I have perused the API JavaDocs some more and may have a solution.

I see that JTextPane is a JEditorPane. The JEditorPane class has setText() and setContentType(). If I set the content type to text/html before I set the text to the raw HTML, it sounds to me as if I should get the rendered HTML.

I can easily read the HTML from the resource, by transferring the data from the stream obtained from ClassLoader.getResourceAsStream() into something like a StringWriter.

Does this sound like a good way to do it?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.
Set the content type to text/html and then call the setPage() or setText() on the JEditorPane.
Also remember to explitly call the editorPane.setEditable(false) as the default is true (yes, really!)

Also you might want to check out this link which talks about putting the Object tag to more use.

http://weblogs.java.net/blog/aim/archive/2007/07/embedding_swing.html

Best of luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!