Render HTML to display in a Java application

Jean-Philippe Pellet picture Jean-Philippe Pellet · Jul 5, 2011 · Viewed 7.2k times · Source

I'm interested in displaying HTML text (using CSS) in a Java app. What options do I have?

Although I don't use SWT in my app, I see SWT provides an interface to a WebKit browser on all platforms. Is there a way I can use SWT's WebKit integration to ask it to render the text in an image that I can reuse?

Answer

Andrew Thompson picture Andrew Thompson · Jul 6, 2011

Swing HTML support

Swing components(1) will render HTML 3.2 and basic styles. It might be a viable option if you control the HTML.

HTML

See my answer here for 2 quick examples of using images in HTML

Direct from a Jar file

JEditorPane displaying Jar'd HTML

Using relative references

Image referenced from base

CSS

For examples of styling HTML in Swing components using CSS, see the answers to:

Java: HTML in Swing, link margin not working

Link Cloud

Word Wrap in JButtons

Word wrap @ 160pxWord wrap @ 200px

Caveat

  1. More specifically, some Swing components support HTML. The 1st screen shot is from a JEditorPane, the rest use a JLabel. Many components use a JLabel as the default rendering component, such as tool tips, & default list & table renderers. JButton supports HTML partly. It will render incorrectly for a disabled button. JTextPane (styled text) also supports HTML, but JTextArea & JTextField (plain text) do not.

See Also

For more information see How to Use HTML in Swing Components in the Java Tutorial.