GWT FORM WIDGETSForm widgets allow users to input data and provides them interaction capability with the application. Every form widget inherits properties from Widget class which in turn inherits properties from UIObject and Wigdet classes.
Introduction The class UIObject is the superclass for all user-interface objects. It simply wraps a DOM element, and cannot receive events. It provides direct child classes like Widget, MenuItem, MenuItemSeparator, TreeItem.
Following is the declaration for com.google.gwt.user.client.ui.UIObject class:
public abstract class UIObject
extends java.lang.Object Field Following are the fields for com.google.gwt.user.client.ui.UIObject class:
This class inherits methods from the following classes:
Introduction The class Widget is the base class for the majority of user-interface objects. Widget adds support for receiving events from the browser and being added directly to panels. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.Widget class:
public class Widget
extends UIObject implements EventListener Field Following are the fields for com.google.gwt.user.client.ui.Widget class:
Class Methods
This class inherits methods from the following classes:
Following are the few important Form Widgets:
Introduction The Button widget represents a standard push button. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.Button class:
public class Button
extends ButtonBase CSS Style Rules Following default CSS Style rule will be applied to all the Button widget. You can override it as per your requirements.
.gwt-Button { }
Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a Button Widget in GWT. Following steps will help you update the GWT application which we created in GWT - Create Application chapter:
<?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.jtc.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-Button{ color:red; } .gwt-Green-Button{ color:green; } .gwt-Blue-Button { color:blue; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>Button Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of Button widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; 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 buttons Button redButton = new Button("Red"); Button greenButton = new Button("Green"); Button blueButton = new Button("Blue"); // use UIObject methods to set button properties. redButton.setWidth("100px"); greenButton.setWidth("100px"); blueButton.setWidth("100px"); greenButton.addStyleName("gwt-Green-Button"); blueButton.addStyleName("gwt-Blue-Button"); //add a clickListener to the button redButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Red Button clicked!"); } }); //add a clickListener to the button greenButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Green Button clicked!"); } }); //add a clickListener to the button blueButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Blue Button clicked!"); } }); // Add button to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(redButton); panel.add(greenButton); panel.add(blueButton); 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, then this will produce the following result: When you click Click Me button, it will show an alert message saying Hello World! GWT - PushButton Widget Introduction The PushButton widget represents a standard push button with custom styling. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.PushButton class:
public class PushButton
extends CustomButton CSS Style Rules Following default CSS Style rules will be applied to all the PushButton widget. You can override it as per your requirements.
.gwt-PushButton-up {}
.gwt-PushButton-down {} .gwt-PushButton-up-hovering {} .gwt-PushButton-down-hovering {} .gwt-PushButton-up-disabled {} .gwt-PushButton-down-disabled {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a PushButton Widget in GWT. The following steps will help you update the GWT application which we created in GWT - Create Application chapter:
<?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.jtc.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-PushButton{ color:red; } .gwt-PushButton-up { color:green; } .gwt-PushButton-down { color:blue; } .gwt-PushButton-up-hovering { color:pink; } .gwt-PushButton-down-hovering { color:aqua; } .gwt-PushButton-up-disabled { color:lime; } .gwt-PushButton-down-disabled { color:maroon; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>PushButton Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of PushButton widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.PushButton; 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 a push button PushButton pushButton = new PushButton("Click Me!"); //create a push button PushButton pushButton1 = new PushButton("Click Me!"); //disable a push button pushButton1.setEnabled(false); //add a clickListener to the push button pushButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Hello World!"); } }); // Add push buttons to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(pushButton); panel.add(pushButton1); 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 the following result: When you click on Click Me! button, it will show an alert message as Hello World! You can see the color of button text and its state will change with your interaction.
Introduction The ToggleButton widget represents a stylish button which allows the user to toggle between up and down states. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.ToggleButton class:
public class ToggleButton
extends CustomButton CSS Style Rules Following default CSS Style rules will be applied to all the ToggleButton widgets. However, you can override it as per your requirements.
.gwt-ToggleButton-up {}
.gwt-ToggleButton-down {} .gwt-ToggleButton-up-hovering {} .gwt-ToggleButton-down-hovering {} .gwt-ToggleButton-up-disabled {} .gwt-ToggleButton-down-disabled {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a ToggleButton Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-ToggleButton-up { color:green; } .gwt-ToggleButton-down { color:blue; } .gwt-ToggleButton-up-hovering { color:pink; } .gwt-ToggleButton-down-hovering { color:aqua; } .gwt-ToggleButton-up-disabled { color:lime; } .gwt-ToggleButton-down-disabled { color:maroon; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>ToggleButton Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of ToggleButton widget.
public class HelloWorld implements EntryPoint {
public void onModuleLoad() { //create toggle buttons ToggleButton toggleButton = new ToggleButton("Click Me!"); ToggleButton toggleButton1 = new ToggleButton("Click Me!"); //disable a toggle button toggleButton1.setEnabled(false); //add a clickListener to the toggle button toggleButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Hello World!"); } }); // Add toggle button to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(toggleButton); panel.add(toggleButton1); 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, then this will produce the following result: When you click Click Me button, it will show an alert message Hello World! You can see color of button text will change with your interaction.
Introduction The Checkbox widget represents a standard checkbox. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.CheckBox class:
public class CheckBox
extends ButtonBase implements HasName Css Style Rules Following default CSS Style rules will be applied to all the CheckBox widget. You can override it as per your requirements.
.gwt-CheckBox {}
.gwt-CheckBox-disabled {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a CheckBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-CheckBox{ color:green; } .gwt-CheckBox-disabled { color:green; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CheckBox Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of CheckBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.CheckBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { // Make a new check box, and select it by default. CheckBox checkBox1 = new CheckBox("Check Me!"); CheckBox checkBox2 = new CheckBox("Check Me!"); // set check box as selected checkBox1.setValue(true); //disable a checkbox checkBox2.setEnabled(false); checkBox1.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { CheckBox checkBox = (CheckBox)event.getSource(); Window.alert("CheckBox is " + (checkBox.getValue() ? "" : "not") + " checked"); } }); // Add checkboxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(checkBox1); panel.add(checkBox2); 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, then this will produce the following result: When you click Click Me CheckBox, it will show an alert message saying CheckBox is checked or not checked. GWT - Radio Button Widget Introduction The RadioButton widget represents a mutually exclusive selection radio button. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.RadioButton class:
public class RadioButton
extends CheckBox CSS Style Rules Following default CSS Style rules will be applied to all the RadioButton widget. You can override it as per your requirements.
.gwt-RadioButton {}
Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a RadioButton Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-RadioButton{ color:green; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>RadioButton Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of RadioButton widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RadioButton; 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 some radio buttons, all in one group 'radioGroup'. RadioButton radioButton1 = new RadioButton("radioGroup", "First"); RadioButton radioButton2 = new RadioButton("radioGroup", "Second"); RadioButton radioButton3 = new RadioButton("radioGroup", "Third"); // Check 'First' by default. radioButton1.setValue(true); //disable 'Second' radio button radioButton2.setEnabled(false); // Add toggle button to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(radioButton1); panel.add(radioButton2); panel.add(radioButton3); 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, then this will produce the following result: GWT - List Box Widget Introduction The ListBox widget represents a list of choices to the user, either as a list box or as a drop-down list. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.ListBox class:
public class ListBox
extends FocusWidget implements SourcesChangeEvents, HasName Css Style Rules Following default CSS Style rules will be applied to all the ListBox widget. You can override it as per your requirements.
.gwt-ListBox {}
Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a ListBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-ListBox{ color:green; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>ListBox Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of ListBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { // Make a new list box, adding a few items to it. ListBox listBox1 = new ListBox(); listBox1.addItem("First"); listBox1.addItem("Second"); listBox1.addItem("Third"); listBox1.addItem("Fourth"); listBox1.addItem("Fifth"); // Make a new list box, adding a few items to it. ListBox listBox2 = new ListBox(); listBox2.addItem("First"); listBox2.addItem("Second"); listBox2.addItem("Third"); listBox2.addItem("Fourth"); listBox2.addItem("Fifth"); // Make enough room for all five items listBox1.setVisibleItemCount(5); //setting itemcount value to 1 turns listbox into a drop-down list. listBox2.setVisibleItemCount(1); // Add listboxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(listBox1); panel.add(listBox2); 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, then this will produce following result: GWT - Suggestion Box Widget Introduction The SuggestionBox widget represents a text box or text area which displays a pre-configured set of selections that match the user's input. Each SuggestBox is associated with a single SuggestOracle. The SuggestOracle is used to provide a set of selections given a specific query string. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.SuggestionBox class:
public final class SuggestBox
extends Composite implements HasText, HasFocus, HasAnimation, SourcesClickEvents, SourcesFocusEvents, SourcesChangeEvents, SourcesKeyboardEvents, FiresSuggestionEvents Css Style Rules Following default CSS Style rules will be applied to all the ListBox widget. You can override it as per your requirements.
.gwt-SuggestBox { }
.gwt-SuggestBoxPopup { } .gwt-SuggestBoxPopup .item { } .gwt-SuggestBoxPopup .item-selected { } .gwt-SuggestBoxPopup .suggestPopupTopLeft { } .gwt-SuggestBoxPopup .suggestPopupTopLeftInner { } .gwt-SuggestBoxPopup .suggestPopupTopCenter { } .gwt-SuggestBoxPopup .suggestPopupTopCenterInner { } .gwt-SuggestBoxPopup .suggestPopupTopRight { } .gwt-SuggestBoxPopup .suggestPopupTopRightInner { } .gwt-SuggestBoxPopup .suggestPopupMiddleLeft { } .gwt-SuggestBoxPopup .suggestPopupMiddleLeftInner { } .gwt-SuggestBoxPopup .suggestPopupMiddleCenter { } .gwt-SuggestBoxPopup .suggestPopupMiddleCenterInner { } .gwt-SuggestBoxPopup .suggestPopupMiddleRight { } .gwt-SuggestBoxPopup .suggestPopupMiddleRightInner { } .gwt-SuggestBoxPopup .suggestPopupBottomLeft { } .gwt-SuggestBoxPopup .suggestPopupBottomLeftInner { } .gwt-SuggestBoxPopup .suggestPopupBottomCenter { } .gwt-SuggestBoxPopup .suggestPopupBottomCenterInner { } .gwt-SuggestBoxPopup .suggestPopupBottomRight { } .gwt-SuggestBoxPopup .suggestPopupBottomRightInner { } Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a SuggestionBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-SuggestBox { color: green; } .gwt-SuggestBoxPopup { border: thin 1px solid green; width: 200px; } .gwt-SuggestBoxPopup.item { color: red; } .gwt-SuggestBoxPopup .item-selected { color: gray; } .gwt-SuggestBoxPopup .suggestPopupTopLeft { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupTopLeftInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupTopCenter { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupTopCenterInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupTopRight { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupTopRightInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupMiddleLeft { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupMiddleLeftInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupMiddleCenter { border: thin 1px solid green; width:200px; } .gwt-SuggestBoxPopup .suggestPopupMiddleCenterInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupMiddleRight { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupMiddleRightInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomLeft { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomLeftInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomCenter { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomCenterInner { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomRight { border: thin 1px solid green; } .gwt-SuggestBoxPopup .suggestPopupBottomRightInner { border: thin 1px solid green; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>SuggestionBox Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of SuggestionBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.MultiWordSuggestOracle; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.SuggestBox; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { //create the suggestion data MultiWordSuggestOracle oracle = new MultiWordSuggestOracle(); oracle.add("A"); oracle.add("AB"); oracle.add("ABC"); oracle.add("ABCD"); oracle.add("B"); oracle.add("BC"); oracle.add("BCD"); oracle.add("BCDE"); oracle.add("C"); oracle.add("CD"); oracle.add("CDE"); oracle.add("CDEF"); oracle.add("D"); oracle.add("DE"); oracle.add("DEF"); oracle.add("DEFG"); //create the suggestion box and pass it the data created above SuggestBox suggestionBox = new SuggestBox(oracle); //set width to 200px. suggestionBox.setWidth("200"); // Add suggestionbox to the root panel. VerticalPanel panel = new VerticalPanel(); panel.add(suggestionBox); 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, then this will produce the following result: GWT - TextBox Widget Introduction The TextBox widget represents a standard single-line text box. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.TextBox class:
public class TextBox
extends TextBoxBase implements HasDirection CSS Style Rules Following default CSS Style rules will be applied to all the TextBox widget. You can override it as per your requirements.
.gwt-TextBox {}
.gwt-TextBox-readonly {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a TextBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-TextBox { color: green; } .gwt-TextBox-readonly { background-color: yellow; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>TextBox Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of TextBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { //create textboxes TextBox textBox1 = new TextBox(); TextBox textBox2 = new TextBox(); //add text to text box textBox2.setText("Hello World!"); //set textbox as readonly textBox2.setReadOnly(true); // Add text boxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(textBox1); panel.add(textBox2); 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, then this will produce the following result: GWT - Password Text Box Widget Introduction The PasswordTextBox widget represents a standard single-line text box that visually masks its input to prevent eavesdropping. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.PasswordTextBox class:
public class PasswordTextBox
extends TextBox CSS Style Rules Following default CSS Style rules will be applied to all the PasswordTextBox widget. You can override it as per your requirements.
.gwt-PasswordTextBox {}
.gwt-PasswordTextBox-readonly {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a PasswordTextBox Widget in GWT. Follow the following steps to update the GWT application we created inGWT - Create Application chapter:
<?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.jtc.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-PasswordTextBox { color: green; } .gwt-PasswordTextBox-readonly { background-color: yellow; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>PasswordTextBox Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of PasswordTextBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.PasswordTextBox; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { //create password textboxes PasswordTextBox passwordTextBox1 = new PasswordTextBox(); PasswordTextBox passwordTextBox2 = new PasswordTextBox(); //add text to text box passwordTextBox2.setText("hell@W@rld"); //set textbox as readonly passwordTextBox2.setReadOnly(true); // Add text boxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(passwordTextBox1); panel.add(passwordTextBox2); 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, then this will produce the following result: GWT - Text Area Widget Introduction The TextArea widget represents a text box that allows multiple lines of text to be entered. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.TextArea class:
public class TextArea
extends TextBoxBase implements HasDirection CSS Style Rules Following default CSS Style rules will be applied to all the TextBox widget. You can override it as per your requirements.
.gwt-TextArea {}
.gwt-TextArea-readonly {} Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a TextBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:
<?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.jtc.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-TextArea { color: green; } .gwt-TextArea-readonly { background-color: yellow; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>TextArea Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of TextBox widget.
package com.jtc.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 HelloWorld 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 World! \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); } } 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, then this will produce the following result: GWT - Rich Text Area Widget Introduction The RichTextArea widget represents a rich text editor that allows complex styling and formatting. As some browsers do not support rich text editing, and others support only a limited subset of functionality, there are two formatter interfaces accessed via getBasicFormatter() and getExtendedFormatter(). A browser which does not support rich text editing at all will return null for both of these and one that supports only the basic functionality will return null for the latter getExtendedFormatter(). Class Declaration Following is the declaration for com.google.gwt.user.client.ui.RichTextArea class:
public class RichTextArea
extends FocusWidget implements HasHTML, HasInitializeHandlers, HasSafeHtml CSS Style Rules Following default CSS Style rules will be applied to all the TextBox widget. You can override it as per your requirements.
.gwt-RichTextArea {}
Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a RichTextBox Widget in GWT. The following steps will help you update the GWT application which we created in GWT - Create Application chapter:
<?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.jtc.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-RichTextArea { padding:10px; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>RichTextArea Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of TextBox widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RichTextArea; 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 RichTextArea elements RichTextArea richTextArea = new RichTextArea(); richTextArea.setHeight("200"); richTextArea.setWidth("200"); //add text to text area richTextArea.setHTML("<b>Hello World!</b> <br/> <br/>" + "<i>Be Happy!</i> </br> <br/> <u>Stay Cool!</u>"); // Add text boxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.add(richTextArea); 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, then this will produce following result: GWT - File Upload Widget Introduction The FileUpload widget wraps the HTML <input type='file'> element. This widget must be used with FormPanel if it is to be submitted to a server. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.FileUpload class:
public class FileUpload
extends Widget implements HasName, HasChangeHandlers Css Style Rules Following default CSS Style rules will be applied to all the TextBox widget. You can override it as per your requirements.
.gwt-FileUpload {}
Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a FileUpload Widget in GWT. The following steps will help you update the GWT application which is created in GWT - Create Application chapter:
<?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.jtc.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-FileUpload { color: green; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>FileUpload Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of FileUpload widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.FileUpload; import com.google.gwt.user.client.ui.FormPanel; import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent; 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() { VerticalPanel panel = new VerticalPanel(); //create a FormPanel final FormPanel form = new FormPanel(); //create a file upload widget final FileUpload fileUpload = new FileUpload(); //create labels Label selectLabel = new Label("Select a file:"); //create upload button Button uploadButton = new Button("Upload File"); //pass action to the form to point to service handling file //receiving operation. form.setAction("http://www.jtc.com/gwt/myFormHandler"); // set form to use the POST method, and multipart MIME encoding. form.setEncoding(FormPanel.ENCODING_MULTIPART); form.setMethod(FormPanel.METHOD_POST); //add a label panel.add(selectLabel); //add fileUpload widget panel.add(fileUpload); //add a button to upload the file panel.add(uploadButton); uploadButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { //get the filename to be uploaded String filename = fileUpload.getFilename(); if (filename.length() == 0) { Window.alert("No File Specified!"); } else { //submit the form form.submit(); } } }); form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() { @Override public void onSubmitComplete(SubmitCompleteEvent event) { // When the form submission is successfully completed, this //event is fired. Assuming the service returned a response //of type text/html, we can get the result text here Window.alert(event.getResults()); } }); panel.setSpacing(10); // Add form to the root panel. form.add(panel); RootPanel.get("gwtContainer").add(form); } } 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, then this will produce the following result: Following is the java server page code snippet demonstrating server side capability for file upload. We're using Common IO and Commons FileUpload libraries to add file-upload capability to server side page. File will be uploaded to uploadFiles folder relative to location where upload.jsp is located on server side.
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <%@page import="org.apache.commons.fileupload.FileItem"%> <%@page import="org.apache.commons.io.FilenameUtils"%> <%@page import="java.util.List"%> <%@page import="java.util.Iterator"%> <%@page import="java.io.File"%> <%@page import="java.io.FileOutputStream"%> <%@page import="java.io.InputStream"%> <% // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); try{ // Parse the request List items = upload.parseRequest(request); // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); //handling a normal form-field if(item.isFormField()) { System.out.println("Got a form field"); String name = item.getFieldName(); String value = item.getString(); System.out.print("Name:"+name+",Value:"+value); } else {//handling file loads System.out.println("Not form field"); String fieldName = item.getFieldName(); String fileName = item.getName(); if (fileName != null) { fileName = FilenameUtils.getName(fileName); } String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); System.out.print("Field Name:"+fieldName +",File Name:"+fileName); System.out.print("Content Type:"+contentType +",Is In Memory:"+isInMemory+",Size:"+sizeInBytes); byte[] data = item.get(); fileName = getServletContext() .getRealPath( "/uploadedFiles/" + fileName); System.out.print("File name:" +fileName); FileOutputStream fileOutSt = new FileOutputStream(fileName); fileOutSt.write(data); fileOutSt.close(); out.print("File Uploaded Successfully!"); } } } catch(Exception e){ out.print("File Uploading Failed!" + e.getMessage()); } %> GWT - Hidden Widget Introduction The Hidden widget represents a hidden field in an HTML form. Class Declaration Following is the declaration for com.google.gwt.user.client.ui.Hidden class:
public class Hidden
extends Widget implements HasName Class Constructors
This class inherits methods from the following classes:
This example will take you through simple steps to show usage of a Hidden Widget in GWT. Follow the following steps to update
<?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.jtc.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; } Following is the content of the modified HTML host file war/HelloWorld.html.
<html>
<head> <title>Hello World</title> <link rel="stylesheet" href="HelloWorld.css"/> <script language="javascript" src="helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>Hidden Widget Demonstration</h1> <div id="gwtContainer"></div> </body> </html> Let us have following content of Java file src/com.jtc/HelloWorld.java which will demonstrate use of Hidden widget.
package com.jtc.client;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Hidden; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; public class HelloWorld implements EntryPoint { public void onModuleLoad() { //create textboxes final TextBox textBox = new TextBox(); textBox.setWidth("275"); Button button1 = new Button("Set Value of Hidden Input"); Button button2 = new Button("Get Value of Hidden Input"); final Hidden hidden = new Hidden(); button1.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { hidden.setValue(textBox.getValue()); Window.alert("Value of Hidden Widget Updated!"); } }); button2.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert("Value of Hidden Widget: " + hidden.getValue()); } }); // Add widgets to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(textBox); panel.add(button1); panel.add(hidden); panel.add(button2); 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: |