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) ======================================================================
|