Render HTML to display in a Java application [closed]
开发者_如何学运维
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionI'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?
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
Using relative references
CSS
For examples of styling HTML in Swing components using CSS, see the answers to:
Java: HTML in Swing, link margin not working
Word Wrap in JButtons
Caveat
- More specifically, some Swing components support HTML. The 1st screen shot is from a
JEditorPane
, the rest use aJLabel
. Many components use aJLabel
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, butJTextArea
&JTextField
(plain text) do not.
See Also
For more information see How to Use HTML in Swing Components in the Java Tutorial.
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());
精彩评论