Table of contents
1.
Introduction
2.
Class Declaration
3.
CSS Style Guidelines
4.
Constructors
5.
Methods for GWT TextArea Widget
6.
Inherited Methods
7.
 GWT TextArea Widget Example
7.1.
XML
7.2.
CSS
7.3.
HTML
7.4.
Java
8.
Output
9.
Frequently Asked Questions 
9.1.
What is the <h1> tag?
9.2.
What is Constructor in Java?
9.3.
What is AJAX Application?
9.4.
What is GWT weight?
9.5.
What distinguishes a text field from a text area?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

GWT TextArea Widget

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

Introduction

An open-source collection of tools called Google Web Toolkit (GWT) enables web developers to build and maintain Java-based front-end JavaScript applications.
In this article, we will study GWT TextArea Widget in Google Web Toolkit in detail with its constructors and methods.

GWT Textarea
  • A multi-line text input control is defined by the HTML <textArea> tag.
     
  • The texts are displayed in a fixed-width font and can contain an unlimited number of characters (usually courier).
     
  • The <cols> and <rows> attributes of HTML Textarea control its size, but the height and width properties of CSS can also be used to control it.

Class Declaration

Declaration for com.google.gwt.user.client.ui.TextArea class −

public class TextArea
   extends TextBoxBase
      implements HasDirection

CSS Style Guidelines

The following CSS style rules will be applied to all TextBox widgets. You can override it to suit your needs.

.gwt-TextArea {}
.gwt-TextArea-readonly {}

Constructors

Constructor Modifier Description
TextArea() - Creates an empty text area.
TextArea(Element element) Protected Subclasses can use this function Object() { [native code] } to explicitly use an existing element.

Methods for GWT TextArea Widget

Method Modifier Description
getCharacterWidth() Int Gets the text box's requested width (this is not an exact value, as not all characters are created equal).
getSelectionLength() Int The length of the current text selection is returned.
getCursorPos() Int It gets the current position of a  cursor (this also serves as the beginning of the text selection).
getVisibleLines() Int Gets the number of to text lines that are visible.
setCharacterWidth(int width) Void Sets the text box's requested width (this is not an exact value, as not all characters are created equal).
setVisibleLines(int lines) Void Sets the number of visible text lines.
wrap(Element element) Static TextArea Creates a TextArea widget that wraps an existing <textarea> element.

Inherited Methods

We've already seen many of the TextArea Widget's commonly used methods. Because not all methods can be implemented for every class, a few methods from this class are inherited by other classes.

This GWT TextArea Widget class inherits methods from the classes listed below:

  • com.google.gwt.user.client.ui.UIObject
  • com.google.gwt.user.client.ui.Widget
  • com.google.gwt.user.client.ui.FocusWidget
  • com.google.gwt.user.client.ui.TextBoxBase
  • java.lang.Object

 GWT TextArea Widget Example

1. As described in the GWT - Create Application chapter, create a project called ‘HelloNinjas’ under the package com.codingNinjas.

2. Make the modifications listed below to ‘HelloNInjas.gwt.xml’, ‘HelloNinjas.css’, ‘HelloNinjas.html’, and ‘HelloNinjas.java’. Leave the remaining files alone.

3. Run the application after compiling it to check the logic's outcome.

XML

The modified module descriptor's content is as follows for ‘src/com.codingNinjas/HelloNinjas.gwt.xml’.

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'HelloNinjas'>
   <!-- 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.codingNinjas.client.HelloNinjas'/>
   <!-- Specify the paths for translatable code  -->
   <source path = 'client'/>
   <source path = 'shared'/>
</module>

CSS

Modified Style Sheet file ‘war/HelloNinjas.css’ is as follows.

body
{
   text-align: center;
   font-family: verdana, sans-serif;
}

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

.gwt-TextArea 
{
   color: Blue; 
}

.gwt-TextArea-readonly 
{
   background-color: Red;
}

HTML

Modified HTML host file ‘war/HelloNinjas.html’ is as follows.

<!DOCTYPE html>
<html>
   <head>
      <title>Hello Ninjas</title>
      <link rel = "stylesheet" href = "HelloNinjas.css"/>
      <script language = "javascript" src = "helloninjas/helloninjas.nocache.js">
      </script>
   </head>
   
   <body>
      <h1>TextArea Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

Java

Let us look at the Java file ‘src/com.codingninjas/HelloNinjas.java’, which shows how to use the textbox widget.

package com.codingNinjas.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
public class HelloNinjas implements EntryPoint 
{
   public void onModuleLoad() 
   {
      // Create textarea elements
      TextArea textArea1 = new TextArea(); 
      TextArea textArea2 = new TextArea();

      // Set width as 10 characters
      textArea1.setCharacterWidth(20);
      textArea2.setCharacterWidth(20);
     
      // Set height as 5 lines
      textArea1.setVisibleLines(5);
      textArea2.setVisibleLines(5);
      
      // Add text to text area
      textArea2.setText(" Hello Ninjas! \n Be Happy! \n Stay Cool!");

      // Set textbox as readonly
      textArea2.setReadOnly(true);

      // Add text boxes to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(textArea1);
      panel.add(textArea2);
      RootPanel.get("gwtContainer").add(panel);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

In the output there are 2 text areas are present, first one is for the user to input the text and second is for just a read only text area.

Writing in Textbox

Output gif

Frequently Asked Questions 

What is the <h1> tag?

HTML headings are specified using the <h> to <h6>tags. The primary heading is indicated by the tag <h1>.

What is Constructor in Java?

A Constructor in Java is a unique method that initializes the object.

What is AJAX Application?

A group of technologies known as Asynchronous JavaScript and XML (Ajax) is used to create web applications.

What is GWT weight?

The maximum weight of cargo that a vessel can carry is known as its gross weight tonnage (GWT).

What distinguishes a text field from a text area?

A text field only has one line, whereas a text area typically has multiple lines, which is the main distinction between a text area and a text field ().

Conclusion

In this article, we have studied about GWT TextArea Widget. We have also discussed the constructors as well as the properties of the TextArea Widget class in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding GWT TextArea Widget with its constructors and its properties if you would like to learn more, check out our articles on 

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.

Thank you

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass