United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4406598 : Background attribute in JTextPane is not resolved to the parent style

Details
Type:
Bug
Submit Date:
2001-01-21
Status:
Open
Updated Date:
2011-02-09
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
generic
Sub-Component:
javax.swing
CPU:
generic
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.3.0
Targeted Versions:

Related Reports

Sub Tasks

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
                                     
2006-06-08
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);
	  }
  }
======================================================================
                                     
2004-09-25



Hardware and Software, Engineered to Work Together