简体   繁体   中英

How to render basic HTML markup inside a JPanel in Java Swing?

I want to do something really simple like this:

JPanel htmlPanel = new HtmlPanel("<html><body><h1>hello world</h1></body></html>");

I think I had seen code somewhere that did exactly this. What about CSS and JS? Can the JPanel be made to support this easily? Are there libraries somewhere that do what I am asking for?

Check here to see how to add CSS support to your JEditorPane . You will need an HTMLEditorKit .

I do not see why you would need JavaScript support for a JEditorPane. The basic use of it is to render Help pages written in HTML and CSS.

If you need to have the real McCoy embedded into your application, try the Lobo Java Web Browser . Lobo does JavaScript and CSS and is open source 100% Java.

November 2015 : The latest fork of the Lobo Browser is Lobo Evolution .

Sounds like you were thinking of the JEditorPane . It can display html from eg an URL. Swing tutorial, Editor panes.

The support for javascript is nonexistent.

You can try out third party components such as flying-saucer , which is a XML/XHTML and CSS 2.1 renderer in pure Java.

In case anyone is still looking for an easy solution to just enable display of HTML-Markup: Use a JLabel and enclose the text in '<html>' and '</html>' (body tag isn't needed).

Have a look at How to Use HTML in Swing Components . I just came across this when looking for an easy way to display data in tables, and it works like a charm.

There was JWebPane , a Java component based on WebKit, but that project seems to have died a long time ago and/or has just gone quiet. Some people are still hoping it will be released, The other one I know of is WebKit for SWT , but it appears to be for Win32 only. :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM