Dan A.S.
Dan A.S.

Reputation: 701

JTextPane - HTMLDocument: when adding/removing a new style, other attributes also changes

I have a JTextPane (or JEditorPane) in which I want to add some buttons to format text (as shown in the picture).

When I change the selected text to Bold (making a new Style), the font family (and others attributes) also changes. Why? I want to set (or remove) the bold attribute in the selected text and other stays unchanged, as they were.

This is what I'm trying:

private void setBold(boolean flag){
    HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
    int start = editorPane.getSelectionStart();
    int end = editorPane.getSelectedText().length();

    StyleContext ss = doc.getStyleSheet();

    //check if BoldStyle exists and then add / remove it
    Style style = ss.getStyle("BoldStyle");                       
    if(style == null){
        style = ss.addStyle("BoldStyle", null);
        style.addAttribute(StyleConstants.Bold, true);
    } else {                
        style.addAttribute(StyleConstants.Bold, false);
        ss.removeStyle("BoldStyle");
    }

    doc.setCharacterAttributes(start, end, style, true);
}

But as I explained above, other attributes also change:

Any help will be appreciated. Thanks in advance!

enter image description here

http://oi40.tinypic.com/riuec9.jpg

Upvotes: 1

Views: 843

Answers (2)

Cristiano Lima
Cristiano Lima

Reputation: 1

I made some minor modifications to your code and it worked here:

private void setBold(boolean flag){
    HTMLDocument doc = (HTMLDocument) editorPane.getDocument();

    int start = editorPane.getSelectionStart();
    int end = editorPane.getSelectionEnd();

    if (start == end) {
        return;
    }

    if (start > end) {
        int life = start;
        start = end;
        end = life;
    }

    StyleContext ss = doc.getStyleSheet();

    //check if BoldStyle exists and then add / remove it
    Style style = ss.getStyle(editorPane.getSelectedText());                       
    if(style == null){
        style = ss.addStyle(editorPane.getSelectedText(), null);
        style.addAttribute(StyleConstants.Bold, true);
    } else {                
        style.addAttribute(StyleConstants.Bold, false);
        ss.removeStyle(editorPane.getSelectedText());
    }

    doc.setCharacterAttributes(start, end - start, style, true);

}

Upvotes: 0

HQCasanova
HQCasanova

Reputation: 1158

What you are trying to do can be accomplished with one of the following two lines of code:

new StyledEditorKit.BoldAction().actionPerformed(null);

  or

editorPane.getActionMap().get("font-bold").actionPerformed(null);

... where editorPane is an instance of JEditorPane of course. Both will seamlessly take care of any attributes already defined and supports text selection.

Regarding your code, it does not work with previously styled text because you are overwriting the corresponding attributes with nothing. I mean, you never gather the values for the attributes already set for the current selected text using, say, the getAttributes() method. So, you are effectively resetting them to whatever default the global stylesheet specifies.

The good news is you don't need to worry about all this if you use one of the snippets above. Hope that helps.

Upvotes: 1

Related Questions