How do I format my HTML correctly with an external CSS file?

I want to download an HTML document from the web, display it on a JEditorPane and create it in Java using both an external CSS file and / or any tags <style>...</style>

. What I am doing right now is using jEditorPane.setPage(URL);

and it is not spelled correctly.

-7


source to share


2 answers


Based on JavaDoc - jEditorPane supports HTML 3.2 and CSS1 blood edge so the short answer is you really don't want to try to render modern web pages with it.

However, you can do this:



import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);

URL url = new URL(location of your stylesheet);
StyleSheet styleSheet = new StyleSheet();
styleSheet.importStyleSheet(url)
kit.setStyleSheet(styleSheet);

      

+3


source


I don't think you can render modern HTML with JEditorPane

. From the docs :

The following content types are known by default:

...

Text / HTML

HTML text. The collection used in this case is the class javax.swing.text.html.HTMLEditorKit

that provides HTML 3.2 support.



HTML 3.2 as defined in the last century, i.e. without CSS / CSS2.

You can use an external HTML rendering library as we know it now. A bit of work on Google there will appear several options, or you can look here .

+1


source







All Articles