Question

I m having trouble using JEditorPane. I want to do left align and right align text on the same line.

Here is my code:

INFO_AREA = new JEditorPane();
    INFO_AREA.setBorder(BorderFactory.createCompoundBorder(BORDER, 
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));
    HTMLEditorKit kit = new HTMLEditorKit();
    INFO_AREA.setEditorKit(kit);
    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule(".alignleft{color : rgb(0,128,25); font-weight: bold; float: left;}");
    styleSheet.addRule(".alignright{color : rgb(0,128,25); font-weight: bold; float: right;}");
    INFO_AREA.setText("<html>" +
            "<center><b><font size=6>Important Information</font></b></center>" 
            "<div id=textbox><p class='alignleft'>left</p><p class='alignright'>right</p></div>" +
            "</html>");
    INFO_AREA.setLocation(305, 10);
    INFO_AREA.setSize(275, 200);
    INFO_AREA.setEditable(false);
    PANE.add(INFO_AREA);

It doesn't work it seems like JEditorPane does not support float.

So maybe anyone has any ideas how I can achieve left align and right align text on the same line?

Was it helpful?

Solution

Swing's HTML renderer is old. It supports HTML 3.2 and CSS 1.0, and it does not even support all capabilities of those specifications.

Looking at the documentation of the CSS class, we see that it lists all of the CSS properties it supports. There is a list of what is supported, following by a list prefaced with "The following are modeled, but currently are not rendered." In that list is the float property.

So, Swing HTML rendering does not currently support the float property. At all.

There is a long-standing open bug, Java Bug ID 4296022, for this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top