Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java gwt richtextarea change font-family

Tags:

java

fonts

gwt

The Object richtextarea of java gwt has as default the font-family 'times new roman' is it somehow possible to change the family to 'Arial'?

like image 449
wasp256 Avatar asked Dec 21 '12 10:12

wasp256


2 Answers

You have several options. You can create a CSS class and set it on the body element of the document inside RichTextArea, or you can set the style attribute on it directly. If you want this change to be consistent throughout your app, I recommend creating a new class and adding an InitializeHandler to it.

public class MyRichTextArea extends RichTextArea {

    public MyRichTextArea() {

        addInitializeHandler(new InitializeHandler() {

            @Override
            public void onInitialize(InitializeEvent ie) {

                Document document = IFrameElement.as(getElement()).getContentDocument();
                BodyElement body = document.getBody();
                body.setAttribute("style", "font-family: Arial Unicode MS,Arial,sans-serif;");
            });
        }
    }
}
like image 118
Andrei Volgin Avatar answered Oct 07 '22 11:10

Andrei Volgin


It is better to use the provided functionality. If you want to change it at creation time, you will need the initializeHandler as mentioned in answer 1:

RichTextArea rta = new RichTextArea();
rta.addInitializeHandler(new InitializeHandler() {
    public void onInitialize(InitializeEvent event) {
            rta.getFormatter().setFontName("\"Trebuchet MS\",Trebuchet,Arial");
            rta.getFormatter().setFontSize(FontSize.SMALL);
            rta.getFormatter().setForeColor("#FF0000");
        }
    });

PS: you need to do this before you add any content to the textarea, or it will only be applied to new input.

like image 24
Highway Avatar answered Oct 07 '22 09:10

Highway