Is there a way to attatch a css file to a jEditorPane?
Asked Answered
S

2

6

Simple enough question: I have a string containing HTML that is being handed off to a JEditorPane for user consumption.

Can I attach a CSS file (or string containing CSS rules) to allow for more specific styling of the text?

Sumptuous answered 17/9, 2009 at 0:8 Comment(0)
S
4

The HTMLEditorKit per default looks for a file default.css - I'm not sure where, though.

Alternatively, this should work:

StyleSheet ss = new StyleSheet();
ss.importStyleSheet(styleSheetURL);
HTMLEditorKit kit = (HTMLEditorKit)jEditorPane.getEditorKit();
kit.setStyleSheet(ss);

However, note that HTMLEditorKit only supports a limited subset of CSS 1.

Sarasvati answered 23/9, 2009 at 14:1 Comment(1)
Editor kits are actually shared across multiple editor panes (which doesn't appear to be mentioned in the API, but if you read the source code you'll see it.) So this code will actually change every JEditorPane in the application, rather than the specific one you want to style.Peterus
W
3

Can't you just include a style tag along with the HTML content in setText()?

e.g.

jEditorPane.setText( "<html><head><style type=\"text/css\">...</style></head><body>...");
Wiskind answered 23/9, 2009 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.