Table of contents
1.
Introduction
2.
HTML Widget👾
2.1.
Class Declaration
3.
Constructors
4.
Methods
5.
Example
6.
Output
7.
Frequently Asked Questions
7.1.
What is a widget in HTML?
7.2.
What is RootPanel GWT?
7.3.
How do I build my own HTML widget?
7.4.
What is GWT Servlet?
7.5.
What does GWT produce?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

GWT HTML Widget

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Google Web Toolkit (GWT) is a development framework for building RICH Internet Applications (RIA). Here are some of its notable characteristics:

GWT

GWT gives developers the option of writing client-side applications in JAVA.

  • The GWT compiler converts JAVA code to JavaScript code.
     
  • The GWT application is cross-browser compatible. GWT generates javascript code that is appropriate for each browser.
     
  • GWT is open source and completely free, and it is used by thousands of developers all over the world. The Apache License 2.0 was released.

HTML Widget👾

You can use the HTML Widget to insert your own HTML(Hyper Text Markup Language) content fragment directly into a page. Other Percussion Widgets generate HTML based on the content you enter, but the HTML Widget allows you to enter HTML directly or paste HTML from local or third-party sources.

HTML Widget

The HTML fragment is rendered when the page is rendered. Since the page already contains the<!DOCTYPE>, <!HTML>, <!BODY>, and <!HEAD> tags, your HTML fragment should not include these tags, or it will not render.

GWT HTML Widget-

In GWT HTML Widget, this widget accepts HTML text and displays it using an <div> element, causing it to be displayed in the block layout.

Class Declaration

For com.google.gwt.user.client.ui.Label class −

public class HTML
   extends Label
      implements HasHTML

Constructors

Below are the constructors for GWT HTML Widget:

Constructor Description
HTML() Makes an empty HTML document.
HTML(java.lang.String html) Creates an HTML document with the specified HTML contents.
protected HTML(Element element) Subclasses may explicitly use an existing element using this constructor.
HTML(java.lang.String html, boolean wordWrap) Creates a widget with the specified contents that can optionally be treated as HTML, and word wrapping can optionally be disabled.
HTML(SafeHtml html, HasDirection.Direction dir) Creates a widget in HTML with the specified contents and direction.
HTML(java.lang.String html, HasDirection.Direction dir) Creates a widget in HTML with the specified HTML contents and direction.

Methods

Method Modifier and Type Description
getHTML() java.lang.String Obtains the HTML content of this object.
setHTML(SafeHtml html) void Sets the HTML known to be secure of this object's contents.
setHTML(SafeHtml html, HasDirection.Direction dir) void Sets the HTML for this object and specifies its direction.
setHTML(java.lang.String html) void Puts the specified HTML as the label's content
setHTML(java.lang.String html, HasDirection.Direction dir) void Applies the specified direction while setting the label's content to the specified HTML.
wrap(Element element) static HTML creates an HTML widget that encloses a div or span element that already exists.

Example

This is the content of the modified module descriptor â€˜src/com.Coding Ninjas Studio/index.gwt.xml’.

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'index'>
   <!-- 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.Coding Ninjas Studio.client.index'/>

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


The content of the modified Style Sheet CSS file is named â€˜style.css’.

*{
     margin : 0;
     padding: 0;
     font-family: monospace;
     box-sizing: border-box;
 
 }
 
 .header{
     width : 90%;
     margin : 0 auto;
     text-align: center;
 }
 
 .heading{
     font-size: 4rem;
     margin: 150px;
     color: darkgray;
 }
 
 .main-body{
     width : 90%;
     margin : 0 auto;
     text-align: center;
 }
 
 .main-text{
     display: inline;
     font-size: 2rem;
     margin: 1rem;
     padding: 0.25rem;
     outline: 1px black solid ;
 }
 
 .second{
     outline : 1px blue solid;
 }
 
.third{
     outline : 1px pink solid;
}


Content of the modified HTML host file named â€˜Index.html’.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style.css">
    <title>Document</title>
</head>

<body>
    <header class="header">
        <h1 class="heading"> Coding Ninjas Example: HTML Widget </h1>
    </header>
 
    <main class="main-body">
        <p class="main-text"><b>Hello, welcome to coding ninja</b> using &ltb&gt tag</p>
        <br></br>
        <p class="main-text second"><i>Happy Learning Ninjas!!</i> using &lti&gt tag</p>
        <br></br>
        <p class = "main-text third"><u> Hope you enjoy learning here!</u>using &ltu&gt tag</p>
    </main>
</body>
</html>


Java file, ‘src/com.Coding Ninjas Studio/Index.java’ will demonstrate the use of the HTML widget.

package com.Coding Ninjas Studio.client;

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

public class Index implements EntryPoint {
      public void onModuleLoad() {
      // Create three HTML widgets
      HTML html1 = new HTML("Hello,welcome to coding ninjas using <b> tag.");
      HTML html2 = new HTML("Happy Learning Ninjas!! using <i> tag.");
      HTML html3 = new HTML("Hope you enjoy learning here! using <u>tag.");
 
      // Use UIObject methods to set HTML widget properties.
      html1.addStyleName("gwt-Black-Border");
      html2.addStyleName("gwt-Blue-Border");
      html3.addStyleName("gwt-Pink-Border");
 
      // Add widgets to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(html1);
      panel.add(html2);
      panel.add(html3);
     
      RootPanel.get("gwtContainer").add(panel);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Here’s the output showing the button in HTML with the help of GWT HTML Widget.

Output

Frequently Asked Questions

What is a widget in HTML?

You can insert a piece of your own HTML content directly into a page using the HTML Widget.

What is RootPanel GWT?

GWT The first or uppermost panel to which all other Widgets are attached is called the RootPanel.

How do I build my own HTML widget?

Go to "Custom Widgets" and choose to add a new HTML widget to create HTML widget. Enter the widget's name in the form that appears. A widget icon of your choosing.

What is GWT Servlet?

GWT offers a few different methods for interacting with a server via HTTP.

What does GWT produce?

The GWT compiler uses GWT.Create for deferred binding.

Conclusion

This blog covered the GWT HTML Widget, and you will get to know the GWT and HTML widgets and their methods and constructor.

To know more about the GWT article, you can go through the articles below:


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thank you

Happy Learning Ninja! 

Live masterclass