|
Quick Lists
|
|
Bug ID:
|
4406598
|
|
Votes
|
12
|
|
Synopsis
|
Background attribute in JTextPane is not resolved to the parent style
|
|
Category
|
java:classes_swing
|
|
Reported Against
|
1.3
|
|
Release Fixed
|
|
|
State
|
3-Accepted,
bug
|
|
Priority:
|
4-Low
|
|
Related Bugs
|
|
|
Submit Date
|
21-JAN-2001
|
|
Description
|
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)
======================================================================
|
|
Work Around
|
[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);
}
}
======================================================================
|
|
Evaluation
|
Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=13400
Posted Date : 2006-06-08 04:02:47.0
|
|
Comments
|
Submitted On 19-MAR-2001
tedyoung
This bug effectively makes it impossible to set the
background color for text on a paragraph-by-paragraph
basis, i.e., you can't use setParagraphAttributes() to set
the background color of a paragraph, you have to use this
workaround or setCharacterAttributes.
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|
|
|
 |