JTextPane with background color
Thursday, 1 December 2011
JTextPane and his ancestors JEditorPane and JTextComponent won't respect the color defined with setBackground(Color) since they display a "styled document" and expect the background color to be defined in the content. You'll always see a white background.
To change the background color (without modifying the content) you have to define attributes for the document:
textPane.setContentType("text/html"); // or any other styled content type textPane.setText("White text on a red background"); // Define a default background color attribute textPane.getStyledDocument().setParagraphAttributes(0, textPane.getDocument().getLength(), background, false); // And remove default (white) margin // Alternative: Leave a 2px border but draw it in the same color
Comments
unknown (not verified)
26. May 2016 - 12:27
Permalink
This doesnt appear to work
This doesnt appear to work either, I presume this is to get the background color to show in the editor?