2

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?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234

2 Answers2

3

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.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

Native Swing allows to use an SWT Browser in Java Swing applications and to mix it with Swing widgets. It supports both WebKit and Xulrunner on several platforms (as well as IE on Windows). Make sure to have a libwebkit or libxulrunner that is compatible with the latest SWT implementation as described in the SWT FAQ The browser engine to use is controlled by an NSOption passed to JWebBrowser:

JWebBrowser geckoWebView = new JWebBrowser(JWebBrowser.useXULRunnerRuntime());
JWebBrowser webkitWebView = new JWebBrowser(JWebBrowser.useWebkitRuntime());
jso
  • 31
  • 2