Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

JTextPane ignores CSS

Thread view: 
SPC - 02 Dec 2003 11:23 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.