JDK-4406598 : Background attribute in JTextPane is not resolved to the parent style
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2001-01-21
  • Updated: 2018-09-05
Description

Name: boT120536			Date: 01/21/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

JTextPane ignores the background attribute if it is not present in a style used
to set character attributes even if one parent of that style defines the
background attribute.

The following scenario can demonstrate this:
1) define Style parentStyle;
2) set the background for it
3) define Style childStyle = myStyleContext.addStyle("child", parentStyle);
4) use the child style in a JTextPane: the background attribute will be ignored

Here's a piece of code that does just that:

-------------------------------
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;


public class Scratch extends JFrame {
  JTextPane textPane = new JTextPane();

  public Scratch() {
    textPane.setText("Test test test test test");
    this.getContentPane().add(textPane, BorderLayout.CENTER);

    Style parent = textPane.addStyle("parent", textPane.getStyle("default"));
    StyleConstants.setBackground(parent, Color.red);
    StyleConstants.setForeground(parent, Color.blue);

    Style child = textPane.addStyle("child", parent);

    textPane.select(0, 10);
    textPane.setCharacterAttributes(child, false);
    textPane.select(0,0);
  }

  public static void main(String[] args) {
    Scratch scratch1 = new Scratch();
    scratch1.setSize(new Dimension(300, 300));
    scratch1.setVisible(true);
  }

}
----------------------------

This code shows that the background attribute is ignored while the foreground
one is not although both of them are actually defined by the parent of the
style used for formatting.
(Review ID: 115495) 
======================================================================

Comments
EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=13400
08-06-2006

WORK AROUND Name: boT120536 Date: 01/21/2001 [copy from internal id 115450] Use the following classes to change the default behaviour. Note that you will need to set the editor kit for the JTextPane to new CustomStyledEditorKit() and that you will need to set the background color for the default style to something useful (on windows it is black which would make the JTextPane display balck on black). textPane.setEditorKit(new CustomStyledEditorKit()); Style defaultStyle = textPane.getStyle("default"); StyleConstants.setBackground(defaultStyle, Color.white); Classes: ------------------ public class CustomLabelView extends javax.swing.text.LabelView{ public CustomLabelView(Element elem){ super(elem); } public Color getBackground() { AttributeSet attr = getAttributes(); if (attr != null) { javax.swing.text.Document d = super.getDocument(); if (d instanceof StyledDocument){ StyledDocument doc = (StyledDocument) d; return doc.getBackground(attr); }else{ return null; } } return null; } } public class CustomStyledEditorKit extends StyledEditorKit{ private final ViewFactory defaultFactory = new CustomStyledViewFactory(); public ViewFactory getViewFactory() { return defaultFactory; } } public class CustomStyledViewFactory implements ViewFactory{ public View create(Element elem) { String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new CustomLabelView(elem); }else if (kind.equals (AbstractDocument.ParagraphElementName)) { return new ParagraphView(elem); }else if (kind.equals(AbstractDocument.SectionElementName)) { return new BoxView(elem, View.Y_AXIS); }else if (kind.equals(StyleConstants.ComponentElementName)) { return new ComponentView(elem); }else if (kind.equals(StyleConstants.IconElementName)) { return new IconView(elem); } } // default to text display return new CustomLabelView(elem); } } ======================================================================
25-09-2004