How to set style for RichTextBox?

99 views
Skip to first unread message

Alex Luya

unread,
Aug 10, 2012, 10:19:51 AM8/10/12
to google-we...@googlegroups.com
RichTextBox has such dom level hierarchy?
----------------------------------------------------------------
<iframe>
   <html>
      <body>
      </body>
   </html>
</iframe>
---------------------------------------------------------------

I found setting style name to iframe doesn't work,and It must be set to body element.
I tried to do it like this(in constructor):
----------------------------------------------
     Element body = ((FrameElement) getElement().cast()).getContentDocument().getBody();
     body.setClassName("richTextArea");
----------------------------------------------

A NullPointerException got thrown out on this boy element,and even I wrapper them like this:

----------------------------
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
         @Override
public void execute()
{
             Element body = ((FrameElement) getElement().cast()).getContentDocument().getBody();
             body.setClassName("richTextArea");
}

});

Can anyone give a solution?

Joseph Lust

unread,
Aug 10, 2012, 3:57:25 PM8/10/12
to google-we...@googlegroups.com
iframe has no style property according to the spec, so that is expected.

Can you break it down further? There are about method 5 calls there that could throw NPE, Which one is causing it?


Sincerely,
Joseph

Andrei

unread,
Aug 11, 2012, 10:44:41 AM8/11/12
to google-we...@googlegroups.com
If you want to style body element within the RichTextArea itself, you can do this:

public MyRichTextArea() {
    addInitializeHandler(new InitializeHandler() {
        public void onInitialize(InitializeEvent ie) {
            BodyElement body = IFrameElement.as(getElement()).getContentDocument().getBody();
            body.addClassName("myRichTextAreaClass");
        }
    });
}

Andrei

unread,
Aug 11, 2012, 11:01:02 AM8/11/12
to google-we...@googlegroups.com
Alex,

I forgot to mention that you can add a style to the body element as I shown, but it will do nothing. The reason is that the HTML document inside the RichTextArea does not have access to your CSS file. You can solve it this way (I use this solution, so I tested it):

public MyRichTextArea() { 
    addInitializeHandler(new InitializeHandler() {
        public void onInitialize(InitializeEvent ie) {

            Document document = IFrameElement.as(getElement()).getContentDocument();
            BodyElement body = document.getBody();
            HeadElement head = HeadElement.as(Element.as(body.getPreviousSibling()));
            LinkElement styleLink = document.createLinkElement();
            styleLink.setType("text/css");
            styleLink.setRel("stylesheet"); 
            styleLink.setHref("MyRichTextArea.css");
            head.appendChild(styleLink);
        }
    }); 

}
Reply all
Reply to author
Forward
0 new messages