Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Body background color in HTMLDocument

Started by MickeyMouse Jun 14, 2005 at 4:37 AM 3 replies 1.9k views
Original Post
MickeyMouse
MickeyMouse
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.
Maciej Sawitus
my blog | my games
MickeyMouse
MickeyMouse
:)
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.
Maciej Sawitus
my blog | my games
cra5h
cra5h
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?
MickeyMouse
MickeyMouse
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) {		super(styles);	}	public void addAttributes(Element e, AttributeSet attributes) {		if (e == null || attributes == null)			return;		try {			writeLock();			MutableAttributeSet mattr = (MutableAttributeSet) e					.getAttributes();			mattr.addAttributes(attributes);			fireChangedUpdate(new DefaultDocumentEvent(0, getLength(),					DocumentEvent.EventType.CHANGE));		} finally {			writeUnlock();		}	}}


Added custom HTML editor kit:
public class CustomHTMLEditorKit extends HTMLEditorKit {	public Document createDefaultDocument() {		StyleSheet styles = getStyleSheet();		StyleSheet ss = new StyleSheet();		ss.addStyleSheet(styles);		MutableHTMLDocument doc = new MutableHTMLDocument(ss);		doc.setParser(getParser());		doc.setAsynchronousLoadPriority(4);		doc.setTokenThreshold(100);		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 colorMutableAttributeSet 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 :(

Cheers!
Maciej Sawitus
my blog | my games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.