GWT - Label Widget

The Label can contains only arbitrary text and it can not be interpreted as HTML. This widget uses a <div> element, causing it to be displayed with block layout.
Following is the declaration for com.google.gwt.user.client.ui.Label class −
public class Label 
   extends Widget 
      implements HasHorizontalAlignment, HasText, HasWordWrap, 
         HasDirection, HasClickHandlers, SourcesClickEvents, 
            SourcesMouseEvents, HasAllMouseHandlers
Following default CSS Style rule will be applied to all the labels. You can override it as per your requirements.
.gwt-Label { }
Label()
Creates an empty label.
protected Label(Element element)
This constructor may be used by subclasses to explicitly use an existing element.
Label(java.lang.String text)
Creates a label with the specified text.
Label(java.lang.String text, boolean wordWrap)
Creates a label with the specified text.
void addClickListener(ClickListener listener)
Adds a listener interface to receive click events.
void addMouseListener(MouseListener listener)
Adds a listener interface to receive mouse events.
void addMouseWheelListener(MouseWheelListener listener)
Gets this widget's parent panel.
HasDirection.Direction	getDirection()
Gets the directionality of the widget.
HasHorizontalAlignment. HorizontalAlignmentConstant getHorizontalAlignment()
Gets the horizontal alignment.
java.lang.String getText()
Gets this object's text.
boolean getWordWrap()
Gets whether word-wrapping is enabled.
void onBrowserEvent(Event event)
 Removes a previously added listener interface.
void removeClickListener(ClickListener listener)
This method is called immediately before a widget will be detached from the browser's document.
void removeMouseListener(MouseListener listener)
Removes a previously added listener interface.
void removeMouseWheelListener(MouseWheelListener listener)
Removes a previously added listener interface.
void setDirection(HasDirection.Direction direction)
Sets the directionality for a widget.
void setHorizontalAlignment(HasHorizontalAlignment. HorizontalAlignmentConstant align)
 Sets the horizontal alignment.
void setText(java.lang.String text)
Sets this object's text.
void setWordWrap(boolean wrap)
Sets whether word-wrapping is enabled.
static Label wrap(Element element)
Creates a Label widget that wraps an existing <div> or <span> element.
This class inherits methods from the following classes −
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Widget
This example will take you through simple steps to show usage of a Label Widget in GWT. Follow the following steps to update the GWT application we created in GWT -  Create Application chapter −
Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml.
<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>

   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.tutorialspoint.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

.gwt-Label{ 
   font-size: 150%; 
   font-weight: bold;
   color:red;
   padding:5px;
   margin:5px;
}

.gwt-Green-Border{ 
   border:1px solid green;
}

.gwt-Blue-Border{ 
   border:1px solid blue;
}
Following is the content of the modified HTML host file war/HelloWorld.html to accomodate two buttons.
<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>Label Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>
Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of Label widget.
package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      // create two Labels
      Label label1 = new Label("This is first GWT Label");
      Label label2 = new Label("This is second GWT Label");

      // use UIObject methods to set label properties.
      label1.setTitle("Title for first Lable");
      label1.addStyleName("gwt-Green-Border");
      label2.setTitle("Title for second Lable");
      label2.addStyleName("gwt-Blue-Border");

      // add labels to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.add(label1);
      panel.add(label2);

      RootPanel.get("gwtContainer").add(panel);
   }
}
Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, this will produce following result −
 Print
 Add Notes
 Bookmark this page