3

I'm noticing that when there's an empty line on an HTML JEditorPane, all previously set styling will go away. For example, see the code sample 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 line, but after parsing, doesn't seem to retain the bold attribute. Also notice that if you run this yourself, put your cursor on line 3, and delete everything on the line, the next characters you type will NOT be bold. I imagine that the leaf elements in the HTMLDocument tree are being eliminated when the text they represent is gone, but this starts to seem like buggy behavior when a user runs it.

Does anybody have any ideas how to get the styling attributes to both parse on empty lines, and be retained when everything on a styled line is deleted?

Thanks! --Andy

AndyI
  • 133
  • 1
  • 5
  • To explain my question a little bit better, I'd like to retain character level styling, even when there are no characters on the line (an empty line). The first example I put above is better for an automated example, but my second question is a better illustration. If I have bold text, delete everything on the line, and then type some new characters, I'd like the new characters to still be bold. That is, I'd like the input attributes on the empty line to retain the styling of what was there, even if it's no longer there. Try something like that in MS Word and it works how you would expect! – AndyI Apr 12 '12 at 20:46
  • I don't think so that happens on MS Word, though if you bring your cursor to already written thingy, then your new character takes the Font from the already written character, once you bring your mouse pointer on the previously written character. The same thing happens in `JEditorPane`, as you run this program try to delete everything then see is it happening otherwise, or I misunderstood you. – nIcE cOw Apr 12 '12 at 21:51
  • Yes, if you look at AttributeTracker in StyledEditorKit, it has a CaretListener that syncs the input attributes in the editor kit when the cursor moves. Cool. Unfortunately, when a line is empty, there are no character elements to hold any attributes, and so the input attributes get set to whatever your defaults are (or whatever is inherited from paragraph elements, stylesheets, etc.) – AndyI Apr 12 '12 at 22:43

1 Answers1

3

Instead of writing it your way, if you write like this, then I guess you will get your behaviour, seems like b & /b, tags are old style, though I am using JRE 1.7 update 3, and the following lines works good on this :

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>";

Try this code, as you run the program, try to press Delete by bringing your cursor to the start, then it will retain Colour GREEN, as that's the colour for the last character which your Mouse Pointer touched, then try to press Backspace by bring your cursor to the very end, then it will retain Colour BLUE, as that's the colour for the last character which your Mouse Pointer touched. Third time, simply try to write a word in each line between the two already supplied lines, there is one line which will display characters in red, try to find that line. I am attaching the image below for clarifying the red line scenario.

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

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • +1 Why not move the style defined in each `p` into the `body` element? (I am very confused about what the OP is trying to achieve here, so may be way off target.) – Andrew Thompson Apr 12 '12 at 08:22
  • Nah, you are so right as always, that's what I thought of as well, just that lack of communication so far with the OP that's why never touched this string yet. – nIcE cOw Apr 12 '12 at 08:40
  • Yeah, I realize the tags are old school, but HTML in Swing seems to be from the same school and that's what gets written out if you apply a bold attribute to a character element and serialize to HTML. Your approach is interesting - applying the attribute to the entire paragraph, but doesn't quite get at what I need, since I'd be applying character level attributes in every line that won't be uniform across the paragraph. I'll try to explain my question a little better above. – AndyI Apr 12 '12 at 20:43
  • Thanks for taking the time with this cOw, but you're adding a style at the paragraph level, which is retained even when the characters are gone. However, this forces **everything** in that paragraph to have that style, at least as a base. What I'm looking for is preserving character level styling. To see what I mean, use this for line 3: `"

    Line 3

    "` Delete everything on the line, and the next character you type will not be green. The solution for me might just be to apply styles to paragraph elements if I think that's what the user intends.
    – AndyI Apr 12 '12 at 22:39
  • You are MOST Welcome and KEEP SMILING :-) , it seems to me the situation you asking for is , you wanted to retain styles for each and every character, well that is to be the case, which can be feasible, but a lengthy one i guess, if you use Map and follow the advice given in the answers to this [thread](http://stackoverflow.com/questions/10117286/formatting-jeditorpanes-cursor-tooltips-links/10117980#10117980) – nIcE cOw Apr 12 '12 at 23:22