2

Wondering if you could help me? I'm trying to create some coding in Java to execute html code in a JEditorPane by implementing CSS.

I get it to implement some of the CSS coding as a rule, but it seems to ignore the "float:" bit in the coding. On JSFiddle (link) I get it to run flawlessly, but on Java/NB it creates the container, left div which is small, and the supposed right div, but it puts it below the left div, instead of to the right of it. What I'm thinking is that Java and HTMLEditorKit as well as StyleSheet doesn't support what I want to do. Can anybody give some clarity on this?

Edit:

It seem that only HTML 3.2 is supported. Is there a possible workaround for this?

import java.awt.Dimension; 
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

@SuppressWarnings("serial")
public class jBulletins extends JPanel {

    javax.swing.JEditorPane bulletinBoard = new javax.swing.JEditorPane();

    public jBulletins() {

        Dimension d = new Dimension();
        d.setSize(800, 600);

        bulletinBoard.setPreferredSize(d);
        bulletinBoard.setContentType("text/html"); // NOI18N

        add(new JScrollPane(bulletinBoard));

        loadBulletins();
    }

    private void loadBulletins() {

        //<editor-fold defaultstate="collapsed" desc="HTML/CSS Script">
        String eBullStyles = "#divMsgs{ "
                + "height: 99px; "
                + "margin: auto; "
                + "background-color: #3399FF; "
                + "} "
                + "#divLeft{ "
                + "float: left; "
                + "width: 110px; "
                + "height: 85px; "
                + "border: 2px #6666ff outset; "
                + "padding: 5px; "
                + "background-color: #189cd8; "
                + "color: #ffffff; "
                + "font-size: 11px; "
                + "font-weight: normal; "
                + "font-family: Tahoma, Geneva, sans-serif; "
                + "font-style: normal; "
                + "text-decoration: inherit; "
                + "} "
                + "#divRight{ "
                + "float: right; "
                + "width: 50px; "
                + "height: 85px; "
                + "border: 2px #6666ff outset; "
                + "background-color: #189cd8; "
                + "padding: 5px; "
                + "color: #ffffff; "
                + "font-size: 11px; "
                + "font-weight: normal; "
                + "font-family: Tahoma, Geneva, sans-serif; "
                + "font-style: normal; "
                //+ "text-decoration: inherit; "
                + "line-height: 1.3em; "
                + "}"; //</editor-fold>

        bulletinBoard.setContentType("text/html");
        bulletinBoard.setText("");

        HTMLEditorKit kit = new HTMLEditorKit();
        bulletinBoard.setEditorKit(kit);

        // add some styles to the html
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule(eBullStyles);

        Document setdoc = kit.createDefaultDocument();
        bulletinBoard.setDocument(setdoc);

        String initial = "<div id=\"divMsgs\">"
                + "<div id=\"divLeft\">"
                + "Hello Bob"
                + "</div>"
                + "<div id=\"divRight\">"
                + "How are you today?"
                + "</div>"
                + "</div>";

        try {
            Document doc = bulletinBoard.getDocument();
            bulletinBoard.setText(initial);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void createAndShowGui() {
        jBulletins mainPanel = new jBulletins();
        JFrame frame = new JFrame("Bulletin Board");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
Johan Brink
  • 333
  • 5
  • 20

2 Answers2

7

According to the documentation, Swing components support only HTML 3.2.

And, according to this link, some CSS properties are not rendered:

The following describes the CSS properties that are suppored by the rendering engine:

  • font-family
  • font-style
  • font-size (supports relative units)
  • font-weight
  • font
  • color
  • background-color (with the exception of transparent)
  • background-image
  • background-repeat
  • background-position
  • background
  • text-decoration (with the exception of blink and overline)
  • vertical-align (only sup and super)
  • text-align (justify is treated as center)
  • margin-top
  • margin-right
  • margin-bottom
  • margin-left
  • margin
  • padding-top
  • padding-right
  • padding-bottom
  • padding-left
  • padding
  • border-top-style
  • border-right-style
  • border-bottom-style
  • border-left-style
  • border-style (only supports inset, outset and none)
  • border-top-color
  • border-right-color
  • border-bottom-color
  • border-left-color
  • border-color
  • list-style-image
  • list-style-type
  • list-style-position

The following are modeled, but currently not rendered.

  • font-variant
  • background-attachment (background always treated as scroll)
  • word-spacing
  • letter-spacing
  • text-indent
  • text-transform
  • line-height
  • border-top-width (this is used to indicate if a border should be used)
  • border-right-width
  • border-bottom-width
  • border-left-width
  • border-width
  • border-top
  • border-right
  • border-bottom
  • border-left
  • border
  • width
  • height
  • float
  • clear
  • display
  • white-space
  • list-style
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
2

JEditorPane only provides HTML 3.2 support, so I bet it doesn't support all the css properties we are used to.

Bradley Kaiser
  • 776
  • 4
  • 16
  • There are libraries available that are more advanced than JEditorPane. See this: http://stackoverflow.com/questions/2438201/pure-java-html-viewer-renderer – Bradley Kaiser Aug 05 '14 at 20:08