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

烂漫一生 提交于 2019-12-18 13:37:20

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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. :(



来源:https://stackoverflow.com/questions/6360744/how-to-render-basic-html-markup-inside-a-jpanel-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!