-7

I want to be able to load an HTML document from the Internet, display it in a JEditorPane and have it styled in Java using both an external CSS file and/or from any <style>...</style> tags. What I'm doing right now is using jEditorPane.setPage(URL); and it's not correctly styled.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1610406
  • 722
  • 1
  • 13
  • 24
  • There's a lot of moving parts to this question...could you be more specific? – Makoto Dec 28 '12 at 02:20
  • 2
    *"I want to be able to load an HTML document from the Internet, .. correctly styled."* `JEditorPane` has not a snowball's chance in Hades of rendering HTML off the World Wild Web as we might expect to see it in FF or Chrome unless it is from a limited range of documents that we control (and that are very simple). Otherwise use a different component (perhaps something from JavaFX). – Andrew Thompson Dec 28 '12 at 02:20
  • @Makoto I would like to use an external CSS file to style an HTML document off of the internet. – user1610406 Dec 28 '12 at 02:25
  • 2
    Possible duplicate of [How do I style HTML in a JEditorPane?](http://stackoverflow.com/questions/14061665/how-do-i-style-html-in-a-jeditorpane) – Andrew Thompson Dec 28 '12 at 03:04

2 Answers2

3

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

However, you may be able to 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);
Dennis
  • 32,200
  • 11
  • 64
  • 79
1

I don't think you can render modern HTML with a JEditorPane. From the docs:

By default, the following types of content are known:

...

text/html

HTML text. The kit used in this case is the class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.

HTML 3.2, as defined last century, ie no CSS/CSS2.

You could use an external library to render HTML as we know it now. A little bit of google work will turn up a couple of options, or you can look here.

Community
  • 1
  • 1
clstrfsck
  • 14,715
  • 4
  • 44
  • 59