I've been trying to get a JTextPane to do something with a CSS. It
seems to ignore what I supply. This is the smallest non working
example I can manage. The text gets displayed, but with the default
size and font. I've tried feeding the same text into Mozilla Firebird,
Explorer and Opera and none of them have any problems and display text
as I'd expect. I've looked at the CSS class and font-familiy and
font-size appear to be supported.
Can anyone explain what I'm doing wrong?
import javax.swing.*;
public class TestJEP {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane jtp = new JTextPane();
jtp.setContentType("text/html");
jtp.setEditable(false);
jtp.setText(
"<style>" +
"<!--" +
"BODY {font-family: sans-serif; font-size: 3pt} " +
"TABLE {font-family: sans-serif; font-size: 3pt} " +
"-->" +
"</style> " +
"<body> " +
"<b><em>Frog<em></b>" +
"</body>");
f.getContentPane().add(jtp);
f.pack();
f.show();
}
}
Thomas Kellerer - 02 Dec 2003 11:28 GMT
SPC schrieb:
> I've been trying to get a JTextPane to do something with a CSS. It
> seems to ignore what I supply. This is the smallest non working
[quoted text clipped - 30 lines]
> }
> }
I could get (limited) CSS support only by creating my HTMLDocument with a
StyleSheet object.
StyleSheet style = new StyleSheet();
style.importStyleSheet(url_to_style_sheet);
HTMLDocument html = new HTMLDocument(style);
Thomas
SPC - 03 Dec 2003 09:53 GMT
I decided to sidestep the problem and specify the rules directly into
the editor kit style sheet...
((HTMLEditorKit)jtp.getEeditorKit()).getStyleSheet().addRule(...);
Steve
btw, you have to have made sure the text pane knows it's doing html
before doing this (jtp.setContentType("text/html");)
Christian Kaufhold - 03 Dec 2003 14:32 GMT
> I've been trying to get a JTextPane to do something with a CSS. It
> seems to ignore what I supply. This is the smallest non working
[quoted text clipped - 5 lines]
>
> Can anyone explain what I'm doing wrong?
Your HTML is invalid. The title element is missing, therefore the parser's
fault correction does not put the style tag into the head, therefore it
is not parsed.
Christian