Jump to content
  • Advertisement
Sign in to follow this  
MickeyMouse

[java] Body background color in HTMLDocument

This topic is 4878 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
Advertisement
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) {
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 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 :(

Cheers!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!