0

I have a StyledDocument to which I am adding some style. I want to have some default (global) style that works for all the styles I create. For example a global background for all the style, so that I don't have to specify the background for each style.

Here is how I am trying to achieve

    public void setUpStyles() {
        parentMSGStyle = historyPane.addStyle("parentmsgstyle", null);
        userNameStyle = historyPane.addStyle("usernamestyle", parentMSGStyle);
        StyleConstants.setBackground(parentMSGStyle, Color.GRAY);
        StyleConstants.setForeground(userNameStyle, Color.BLUE);
    }

This does not for now. Only the blue color style is working but not the "GRAY" one. I am new to StyledDocuments. Please point me to right direction.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83

1 Answers1

0

After seeing this i myself tried and got me confused too. According to the JAVA DOC of childStyle = addStyle(String nm, parentStyle) should show the following behavior:

Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.

As long as we are not setting background color to the child, it should not override the parent style's attribute anyway.

StyleConstants.setBackground(parentMSGStyle, Color.GREY);
StyleConstants.setForeground(userNameStyle, Color.BLUE);

If we now try to print the document's background color using document.getBackground(userNameStylee), it should print GRAY and it does so. So for unknown reason it is not just doing on the SCREEN. But surprisingly the reverse will work. That is, set the Forground to the parentStyle, and Background to the childStyle.

StyleConstants.setForeground(parentMSGStyle, Color.BLUE);
StyleConstants.setBackground(userNameStyle, Color.GREY);

Don't tell me it is not a solution. :)

Sage
  • 15,290
  • 3
  • 33
  • 38