new line in Label

3,628 views
Skip to first unread message

L Frohman

unread,
Nov 29, 2007, 2:39:34 AM11/29/07
to Google-We...@googlegroups.com
I want to put a new line in the text of a Label. This works in IE, but the new line is ignored (replaced with space)
in Firefox, Safari, and Opera.
 

public

class Test implements EntryPoint {

   public void
onModuleLoad() {

      Label label =

new Label();

      label.setText(

"abc\ndef\nghi");

      RootPanel.get().add(label);

   }

}

 

I changed \n to \r\n, then \n\n, no difference.

I want to display pretty-printed xml in a Label.

Aragos

unread,
Nov 29, 2007, 6:07:12 AM11/29/07
to Google Web Toolkit
You can use the css property "white-space:pre" to achieve this
effect. Alternately you can also use an HTML instead of a Label and
have a <pre> element within. See an example for both methods below.

public void onModuleLoad() {
String content = "abc\ndef\nghi";

// use a pre element in native HTML
HTML html = new HTML("<pre>" + content + "</pre>");
RootPanel.get().add(html);

// set the white space property explicitely on a label
Label label = new Label(content);
DOM.setStyleAttribute(label.getElement(), "whiteSpace", "pre");
RootPanel.get().add(label);

// define a class with the white space property in a css file,
load the css file and apply to label
// .preFormatted {
// white-space: pre;
// }
Label styledLabel = new Label(content);
styledLabel.addStyleName("preFormatted");
RootPanel.get().add(styledLabel);
}

Hope this helps!

Aragos

Fred Sauer

unread,
Nov 29, 2007, 10:08:33 AM11/29/07
to Google-We...@googlegroups.com

Aragos

unread,
Nov 29, 2007, 10:17:31 AM11/29/07
to Google Web Toolkit
Hi Fred,

I think issue 314 doesn't apply since no text is read from a label.
It's usual for browsers (except IE apparently) to "eat" new lines in
text nodes and to treat them as normal spaces. Issue 314 would come
into play if you try to apply a line like the following to text
displayed with "white-space:pre":

label.setText(label.getText() + "new text here");

Aragos

On Nov 29, 3:08 pm, "Fred Sauer" <f...@allen-sauer.com> wrote:
> I think you are seeing issue 314:
> http://code.google.com/p/google-web-toolkit/issues/detail?id=314
>
> On Nov 29, 2007 12:39 AM, L Frohman <lfroh...@gmail.com> wrote:
>
>
>
> > I want to put a new line in the text of a Label. This works in IE, but
> > the new line is ignored (replaced with space)
> > in Firefox, Safari, and Opera.
>
> > *public*
> > * class* Test implements* EntryPoint {*
>
> > * public void** onModuleLoad() {*
>
> > * Label label = *
> > *new** Label();*
>
> > * label.setText(*
> > *"abc\ndef\nghi"**);*
>
> > * RootPanel.get().add(label);*
>
> > * }*
>
> > *}*
>
> > **
>
> > I changed \n to \r\n, then \n\n, no difference.
>
> > I want to display pretty-printed xml in a Label.
>
> --
> Fred Sauer
> f...@allen-sauer.com

Fred Sauer

unread,
Nov 29, 2007, 10:22:38 AM11/29/07
to Google-We...@googlegroups.com

L Frohman

unread,
Nov 29, 2007, 3:16:02 PM11/29/07
to Google-We...@googlegroups.com
Thanks, that fixes it.
Reply all
Reply to author
Forward
0 new messages