Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 712
pathikrit Avatar asked Jun 15 '11 16:06

pathikrit


2 Answers

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.

like image 73
Costis Aivalis Avatar answered Sep 17 '22 13:09

Costis Aivalis


Sounds like you were thinking of the JEditorPane. It can display html from e.g. 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.

like image 25
Kaj Avatar answered Sep 17 '22 13:09

Kaj