Table of contents
1.
Introduction
2.
TextButton Class
3.
CSS Style Rules
4.
Constructors 
5.
Methods
6.
GWT TextBox Widget Example
6.1.
Step 1: Project Creation👨‍💻
6.2.
Step 2: Modification in Files👨‍🔧
6.3.
Step 3: Check the Result🤳🏼
6.4.
Output
7.
Frequently Asked Questions
7.1.
What do you mean by GWT RPC(Remote Procedure Call)?
7.2.
What do you mean by GWT language?
7.3.
What we have to write in GWT XML file?
7.4.
Can we call GWT as framework?
7.5.
What do you mean by GWT.create()?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

GWT TextBox Widget

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

Introduction

A development toolkit for making RICH Internet Applications(RIA) is called Google Web Toolkit (GWT). It gives programmers the option to create client-side Java applications. The Java code is converted to JavaScript code via GWT.

A typical single-line text box is represented by the TextBox widget. ValueBoxBase.setDirectionEstimator(boolean) has control over this functionality. When at least one of the application's locales is right-to-left, this method is automatically available. Calling new TextBox() constructor without any parameters will generate the instance().

GWT TextBox Widget

Recommended Topic, procedure call in compiler design

TextButton Class

In the GWT(Google Web Toolkit) TextBox widget, the TextBox class is declared as given below:

public class TextBox extends TextBoxBase implements HasDirection
You can also try this code with Online Java Compiler
Run Code

The following classes' methods are inherited by the TextBox class:

Methods in TextBox class

The PushButton class's implemented interfaces are listed below:

  • IsEditor<ValueBoxEditor<java.lang.String>>
  • HasAllDragAndDropHandlers
  • HasAllFocusHandlers
  • HasAllGestureHandlers
  • HasAllKeyHandlers
  • HasAllMouseHandlers
  • HasAllTouchHandlers
  • HasBlurHandlers, etc.

CSS Style Rules

All the below-mentioned GWT TextBox widgets will be styled according to the default CSS rules. Following your needs, you can override it.

.gwt-TextBox {}
.gwt-TextBox-readonly {} 

Constructors 

In the GWT TextBox Widget, the following are the constructors mentioned below:

Sr. No. Constructor Description
1. TextBox() It will create an empty text box.
2. TextBox(Element element) Subclasses may explicitly use an existing element using this constructor.

 

Methods

In the GWT TextBox Widget, the following are the methods mentioned below:

Sr. No. Function Description
1. HasDirection.Direction getDirection() Obtains the widget's directionality.
2. int getMaxLength() Gets the text box's maximum permitted length.
3. int getVisibleLength() Obtains the number of characters that are visible in the text box.
4. void setDirection(HasDirection.Direction direction) Sets a widget's directionality.
5. void setMaxLength(int length) Sets the text box's maximum permitted length.
6. void setVisibleLength(int length) Determines the text box's maximum number of visible characters.
7. static TextBox wrap(Element element) Creates a TextBox widget that encloses an <input type='text'> element that already exists.

 

GWT TextBox Widget Example

This example will walk you through some straightforward GWT TextBox Widget usage instructions. To upgrade the GWT application we developed in GWT, follow these steps:

Step 1: Project Creation👨‍💻

Create a project with the name GWTTextBox.

Step 2: Modification in Files👨‍🔧

Modify the ‘GWTTextBox.gwt.xml’, ‘GWTTextBox.css’, ‘GWTTextBox.html’, and ‘GWTTextBox.java’ files as explained below. Keep the rest of the files unchanged.

Step 3: Check the Result🤳🏼

Compile and run the application to check your results.

The changed module description, located at ‘src/com.codingninjas.gwttextbox/GWTTextBox.gwt.xml’, contains the content given below:

<? xml version = "1.0" encoding = "UTF-8" ?>
    <module rename-to='gwttextbox'>
      <inherits name='com.google.gwt.user.User' />
      <!-- for default stylesheet, you can add any style to your project-->
      <!-- <inherits name='com.google.gwt.user.theme.clean.Clean' /> -->

      <entry-point class='com.codingninjas.gwttxtb.client.GWTTextBox' />
      <source path='client' />
      <source path='shared' />
      <!-- allow Super Dev Mode -->
    <add-linker name="xsiframe" />
    </module>


Now write some styling in the CSS file for your project which is located at ‘war/GWTTextBox.css’.

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

h1 {
    font - size: 3em;
    font - weight: bold;
    color: #004080;
    margin: 30px 0px 80px;
    text - align: center;
}

.gwt - TextBox {
    color: yellow;
}

.gwt - TextBox - readonly {
    background - color: blue;
}

Now write in the HTML host file for your project which is located at war/GWTTextBox.html.

<html>
    <head>
        <title>Coding Ninja’s TextBox Example</title>
        <link rel="stylesheet" href="GWTTextBox.css" />
        <script language="javascript" src="gwttextbox/gwttextbox.nocache.js">
        </script>
    </head>

    <body>
        <h1>TextBox Example</h1>
        <div id="handleDiv"></div>
    </body>
</html>


The Java file ‘src/com.codingninjas.gwttextbox/GWTTextBox.java’ with the following contents will show how to utilize the PushButton widget.

In the above example, we can see there are two text boxes in which one is disabled and the other one is enabled to take inputs. In the disabled text box, the text is already passed as “Welcome to Coding Ninjas!” and set as ReadOnly.

package com.codingninjas.gwttxtb;

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

public class GWTTextBox implements EntryPoint {
	public void onModuleLoad() {
		/*
        	To make the textBox
    	*/
    	
    	TextBox txtBox1 = new TextBox();
    	TextBox txtBox2 = new TextBox();
    	
    	/*
    		To set the text into your textBox
		*/
		txtBox2.setText("Welcome to Coding Ninjas!");
		
		/*
			Now setting textBox as 'readOnly'
		*/
		txtBox2.setReadOnly(true);
		
		/*
			Adding the textBox to the 'RootPanel'
		*/
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(10);
		panel.add(txtBox1);
		panel.add(txtBox2);
		RootPanel.get("handleDiv").add(panel);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

When you are finished and have made all the necessary modifications, compile and run the application in development mode. 

This will get the results as shown below:

TextBox Output 

In the above example, we can see there are two text boxes in which one is disabled and the other one is enabled to take inputs. In the disabled text box, the text is already passed as “Welcome to Coding Ninjas!” and set as ReadOnly.

Frequently Asked Questions

What do you mean by GWT RPC(Remote Procedure Call)?

Your web application's client and server components can easily exchange Java objects over HTTP thanks to the GWT RPC framework.

What do you mean by GWT language?

GWT is nothing but Java to JavaScript compiler. It Creates a JavaScript equivalent for the Java programming language.

What we have to write in GWT XML file?

The actual list of Java classes and other resources that are included in the GWT module is specified in the gwt.xml file.

Can we call GWT as framework?

An open source Java software development platform called Google Web Toolkit (GWT) makes it simple to create AJAX apps.

What do you mean by GWT.create()?

It is used by GWT compiler for deferred binding.

Conclusion

In this article, we have discussed the GWT TextBox Widget. We have discussed its constructors, methods, and implementation. GWT TextBox Widget is a part of form widgets that are used in many places while developing applications.

We hope this blog has helped you enhance your knowledge regarding GWT TextBox Widget. To learn more, check out our articles on GWT RadioButton WidgetGWT CheckBox Widget, and GWT SuggesttBox.

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; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thank you

Happy Learning Ninja!

Live masterclass