Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
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.
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