Jump to content
  • Advertisement
Sign in to follow this  

[java] Body background color in HTMLDocument

This topic is 5244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, Does anybody know how to set body background color for HTMLDocument? I've done a lot of googling and all the java-docs I've seen were not really helpful for me. Thanks in advance.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
< body bgcolor="#0000FF"
ur welcom

Share this post

Link to post
Share on other sites
Heh, I knew it...

I mean javax.swing.text.html.HTMLDocument class. Currently I'm doing the following:

HTMLEditorKit kit = new HTMLEditorKit();
document = (HTMLDocument) kit.createDefaultDocument();
Element html = document.getRootElements()[0];
HTMLDocument.BlockElement body = (HTMLDocument.BlockElement) html.getElement(0);

Then I've tried several ways to set body element background color attribute, none with success.

Share this post

Link to post
Share on other sites
I have no experience with what you are doing, but isn't it possible to add a CSS in the head of your generated HTML and specify the background color in the CSS?

Share this post

Link to post
Share on other sites
Thanks, I've just, finally, solved this, but the solution I came to seems an overkill to me. But it works :)

Here it is in case someone was struggling with this too. I've googled pretty many guys asking about how to do it, but have found only one answer in a google cached pages of some java HTML book chapter ;-)

Added custom HTML document:
public class MutableHTMLDocument extends HTMLDocument {
public MutableHTMLDocument(StyleSheet styles) {

public void addAttributes(Element e, AttributeSet attributes) {
if (e == null || attributes == null)
try {
MutableAttributeSet mattr = (MutableAttributeSet) e
fireChangedUpdate(new DefaultDocumentEvent(0, getLength(),
} finally {

Added custom HTML editor kit:
public class CustomHTMLEditorKit extends HTMLEditorKit {
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
MutableHTMLDocument doc = new MutableHTMLDocument(ss);
return doc;

And here's how I set background color:
Color backgroundColor = new Color(229, 252, 226);

CustomHTMLEditorKit kit = new CustomHTMLEditorKit();
document = (MutableHTMLDocument) kit.createDefaultDocument();
Element html = document.getRootElements()[0];
Element body = html.getElement(0);

// Set background color
MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(HTML.Attribute.BGCOLOR, colorToHex(backgroundColor));
document.addAttributes(body, attr);

In moments like this I really hate swing (maybe I should rather hate HTML part design) for its weird way of working and need for going down to low-level operations to make simple things.

Not to mention hundreds of bugs in Sun's HTML handling implementation. I've also read somewhere that Sun is not putting much effort into their HTML development, so the bugs are not a surprise. Many people are having real troubles doing workarounds to make simple things just work :(


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!