-
Bug
-
Resolution: Not an Issue
-
P4
-
6u24
-
x86
-
windows_7
FULL PRODUCT VERSION :
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
A DESCRIPTION OF THE PROBLEM :
If you wanted to customise a JEditorPane's stylesheet, you might do this:
HTMLEditorKit htmlEditorKit = (HTMLEditorKit) getEditorKitForContentType("text/html");
StyleSheet myStyleSheet = new StyleSheet();
myStyleSheet.addStyleSheet(htmlEditorKit.getStyleSheet());
myStyleSheet.addRule("body { margin: 0pt; font: " + font.getSize() + "pt " + font.getFamily() + "; }");
htmlEditorKit.setStyleSheet(myStyleSheet);
Problem is, although the HTMLEditorKit you get back from the call to getEditorKitForContentType might be fresh, the StyleSheet you get from getStyleSheet() is the exact same object you might have already customised. This will result in the following undesirable effects:
1. "Leaking" of stylesheets between multiple editor panes in the one application.
2. StackOverflowError when rendering the text, once you have used a sufficiently large number of editor kits throughout the application's lifecycle.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The left text should be red, and the right text should be black (assuming your default colour is black.)
ACTUAL -
Both are red.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HTMLEditorKitBugTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new HTMLEditorKitBugTest().run();
}
});
}
private void run() {
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
split.setLeftComponent(new JScrollPane(createCustomisedEditorPane()));
split.setRightComponent(new JScrollPane(createNormalEditorPane()));
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(split);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
split.setDividerLocation(0.5);
}
private JEditorPane createCustomisedEditorPane() {
JEditorPane editorPane = new JEditorPane();
HTMLEditorKit editorKit = (HTMLEditorKit) editorPane.getEditorKitForContentType("text/html");
StyleSheet styleSheet = editorKit.getStyleSheet();
styleSheet.addRule("p { color: red }");
editorPane.setContentType("text/html");
editorPane.setText("<p>Should be red.</p>");
return editorPane;
}
private JEditorPane createNormalEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<p>Should be the default colour.</p>");
return editorPane;
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround is probably to write your own cloneHTMLEditorKit() method which works correctly, calling HTMLEditorKit.clone() and then additionally cloning the StyleSheet (which doesn't have clone() so it might have to be done the hard way.)
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
A DESCRIPTION OF THE PROBLEM :
If you wanted to customise a JEditorPane's stylesheet, you might do this:
HTMLEditorKit htmlEditorKit = (HTMLEditorKit) getEditorKitForContentType("text/html");
StyleSheet myStyleSheet = new StyleSheet();
myStyleSheet.addStyleSheet(htmlEditorKit.getStyleSheet());
myStyleSheet.addRule("body { margin: 0pt; font: " + font.getSize() + "pt " + font.getFamily() + "; }");
htmlEditorKit.setStyleSheet(myStyleSheet);
Problem is, although the HTMLEditorKit you get back from the call to getEditorKitForContentType might be fresh, the StyleSheet you get from getStyleSheet() is the exact same object you might have already customised. This will result in the following undesirable effects:
1. "Leaking" of stylesheets between multiple editor panes in the one application.
2. StackOverflowError when rendering the text, once you have used a sufficiently large number of editor kits throughout the application's lifecycle.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The left text should be red, and the right text should be black (assuming your default colour is black.)
ACTUAL -
Both are red.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.BorderLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class HTMLEditorKitBugTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new HTMLEditorKitBugTest().run();
}
});
}
private void run() {
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
split.setLeftComponent(new JScrollPane(createCustomisedEditorPane()));
split.setRightComponent(new JScrollPane(createNormalEditorPane()));
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(split);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
split.setDividerLocation(0.5);
}
private JEditorPane createCustomisedEditorPane() {
JEditorPane editorPane = new JEditorPane();
HTMLEditorKit editorKit = (HTMLEditorKit) editorPane.getEditorKitForContentType("text/html");
StyleSheet styleSheet = editorKit.getStyleSheet();
styleSheet.addRule("p { color: red }");
editorPane.setContentType("text/html");
editorPane.setText("<p>Should be red.</p>");
return editorPane;
}
private JEditorPane createNormalEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText("<p>Should be the default colour.</p>");
return editorPane;
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround is probably to write your own cloneHTMLEditorKit() method which works correctly, calling HTMLEditorKit.clone() and then additionally cloning the StyleSheet (which doesn't have clone() so it might have to be done the hard way.)