Skip to Main Content

HTMLEditorKit and CSS

843804Dec 18 2004 — edited Feb 28 2005
I'm having problems using CSS with HTMLEditorKit . Here it is a small example:
package com.textpane.gui;

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class JTextPaneApp
extends JFrame
{
    public final static int APP_WIDTH = 640;
    public final static int APP_HEIGHT = 400;

    public JTextPane jTextPane;
    public StyleSheet styleSheet;
    public HTMLDocument htmlDocument;
    public HTMLEditorKit htmlEditorKit;

    public javax.swing.text.Element contentElement;

    public JTextPaneApp()
    {
        setSize(APP_WIDTH, APP_HEIGHT);
        setResizable(false);
        setTitle("JTextPane App");

        styleSheet = new StyleSheet();
        styleSheet.addRule("body {font-family: Tahoma; font-size: 11pt; font-style: normal; font-weight: normal;}");
        styleSheet.addRule(".nick {color: blue;}");
        styleSheet.addRule(".normal {color: black;}");

        htmlEditorKit = new HTMLEditorKit();
        htmlEditorKit.setStyleSheet(styleSheet);
        htmlDocument = (HTMLDocument)htmlEditorKit.createDefaultDocument();

        jTextPane = new JTextPane();
        jTextPane.setEditorKit(htmlEditorKit);
        jTextPane.setDocument(htmlDocument);

        try {
            javax.swing.text.Element htmlElement = htmlDocument.getRootElements()[0];
            javax.swing.text.Element bodyElement = htmlElement.getElement(0);
            htmlDocument.insertAfterStart(bodyElement, "<div></div>");

            contentElement = bodyElement.getElement(0);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        contentPane.add(jTextPane, BorderLayout.CENTER);

        addWindowListener
        (
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
        );

        StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<div>");
        sbHtml.append("<font class=\"nick\">").append("Peter said: ").append("</font>");
        sbHtml.append("<font class=\"normal\">").append("Hello, world!").append("</font>");
        sbHtml.append("</div>");

        try {
            htmlDocument.insertBeforeEnd(contentElement, sbHtml.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        try {
            JTextPaneApp jTextPaneApp = new JTextPaneApp();
            jTextPaneApp.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
The string "Peter said: " should be rendered in blue and the string "Hello, world!" should be rendered in black, but everything is rendered in black. The body rule does work but the other rules don't seem to work. Can anybody help me, please?
Comments
Post Details
Added on Dec 18 2004
2 comments
1,628 views