Saving formatting in empty HTML strings in JEditorPane

I notice that when there is an empty line in the HTML JEditorPane, all previously set styles will go away. For example, see the sample code below:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;


public class BlankLineTester {
    private JEditorPane jep;

    public BlankLineTester() {
        String html = "<html><head></head><body>" +
                "<p><b>Line 1</b></p>" +
                "<p><b></b></p>" +
                "<p><b>Line 3</b></p>" +
                "</body></html>";

        jep = new JEditorPane();
        jep.setContentType("text/html");
        jep.setText(html);

        JFrame frame = new JFrame("Blank Line Test");
        frame.getContentPane().add(jep);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        System.out.println("Line 3 is " + isInputAttributeBold());

        jep.getCaret().setDot(8);
        System.out.println("Line 2 is " + isInputAttributeBold());

    }

    private boolean isInputAttributeBold() {
        AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
        return StyleConstants.isBold(attSet);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BlankLineTester();
            }
        });
    }
}

Line 2 is initially set as a bold empty string, but a bold attribute is not saved after parsing. Also note that if you run this yourself, place the cursor on line 3 and delete everything on the line, the next characters you type will NOT be shown in bold. I believe that the sheet elements in the HTMLDocument tree are eliminated when the text they represent is gone, but this starts to seem like erroneous behavior when the user launches it.

- , , ?

! --Andy

+3
1

, -, , , , b & /b, - , JRE 1.7 update 3, :

String html = "<html><head></head><body>" +
                "<p style = \"font-weight:bold\">Line 1</p>" +
                "<p style = \"font-weight:bold\"></p>" +
                "<p style = \"font-weight:bold\">Line 3</p>" +
                "</body></html>";

, , Delete, , Color GREEN, , , Backspace, , Color BLUE, . , , , . .

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;


public class BlankLineTester {
    private JEditorPane jep;

    public BlankLineTester() {
        String html = "<html><head></head><body>" +
                "<p style = \"font-weight:bold; color: blue\">Line 1</p><br />" +
                "<p style = \"font-weight:bold; color: red\"></p><br />" +
                "<p style = \"font-weight:bold; color: green\">Line 3</p>" +
                "</body></html>";

        jep = new JEditorPane();
        jep.setContentType("text/html");
        jep.setText(html);

        JFrame frame = new JFrame("Blank Line Test");
        frame.getContentPane().add(jep);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        System.out.println("Line 3 is " + isInputAttributeBold());

        jep.getCaret().setDot(8);
        System.out.println("Line 2 is " + isInputAttributeBold());

    }

    private boolean isInputAttributeBold() {
        AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
        return StyleConstants.isBold(attSet);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BlankLineTester();
            }
        });
    }
}

FONT STYLE

+3

All Articles