GWT Tutorial PDF
GWT Tutorial PDF
Audience
This tutorial is designed for software professionals who are willing to learn GWT
programming in simple and easy steps. This will also give you great
understanding on GWT Programming concepts.
After completing this tutorial, you will be at intermediate level of expertise from
where you can take yourself at higher level of expertise.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of
Java programming language, text editor, and execution of programs, etc.
Because we are going to develop web-based applications using GWT, it will be
good if you have an understanding of other web technologies such as HTML,
CSS, and AJAX.
GWT
Table of Contents
About the Tutorial ................................................................................................................................. i
Audience ............................................................................................................................................... i
Prerequisites ......................................................................................................................................... i
Copyright and Disclaimer ...................................................................................................................... i
Table of Contents ................................................................................................................................. ii
1.
2.
3.
4.
ii
GWT
Step 3 - Modify Style Sheet: HelloWorld.css ........................................................................................17
Step 4 - Modify Host File: HelloWorld.html .........................................................................................17
Step 5 - Modify Entry Point: HelloWorld.java ......................................................................................18
Step 6 - Compile Application ...............................................................................................................18
Step 7 - Run Application ......................................................................................................................19
5.
6.
7.
8.
iii
GWT
Button Widget Example ......................................................................................................................79
GWT - PushButton Widget ..................................................................................................................83
GWT - ToggleButton Widget ................................................................................................................90
Togglebutton Widget Example ............................................................................................................92
GWT - CheckBox Widget ......................................................................................................................97
CheckBox Widget Example ................................................................................................................100
GWT - RadioButton Widget ...............................................................................................................104
GWT - ListBox Widget ........................................................................................................................108
ListBox Widget Example ....................................................................................................................112
GWT - SuggestionBox Widget ............................................................................................................116
GWT - TextBox Widget ......................................................................................................................126
GWT - PasswordTextBox Widget .......................................................................................................131
GWT - TextArea Widget .....................................................................................................................136
GWT - RichTextArea Widget ..............................................................................................................142
Richtextbox Widget Example ............................................................................................................144
GWT - FileUpload Widget ..................................................................................................................148
GWT - Hidden Widget........................................................................................................................157
Hidden Widget Example ....................................................................................................................159
9.
iv
GWT
GWT - CellTable Widget ....................................................................................................................233
GWT - CellBrowser Widget ................................................................................................................243
GWT
vi
GWT
Internationalization - Complete Example ..........................................................................................466
vii
1. GWT OVERVIEW
GWT
What is GWT?
Google Web Toolkit (GWT) is a development toolkit to create RICH Internet
Applications (RIA). Here are some of its notable features:
GWT
Again being Java based, GWT has a low learning curve for Java
Developers.
GWT provides widgets library which provides most of the tasks required
in an application.
On top of everything, GWT applications can run on all major browsers and
smart phones including Android and iOS based phones/tablets.
GWT
Disadvantages of GWT
Although GWT offers plenty of advantages, it suffers from the following
disadvantages
Not Designer's Friendly: GWT is not suitable for web designers who
prefer using plain HTML with placeholders for inserting dynamic content
at a later point in time.
GWT also provides a GWT Hosted Web Browser which lets you run and execute
your GWT applications in hosted mode, where your code runs as Java in the
Java Virtual Machine without compiling to JavaScript.
2. GWT ENVIRONMENT
GWT
System Requirement
GWT requires JDK 1.6 or higher so the very first requirement is to have JDK
installed in your machine.
JDK
1.6 or above.
Memory
No minimum requirement.
Disk Space
No minimum requirement.
Operating System
No minimum requirement.
Follow the given steps to setup your environment to start with GWT application
development.
Task
Command
Windows
Linux
$ java -version
Mac
Open Terminal
Generated Output
3
GWT
java version "1.6.0_21"
Windows
Linux
Mac
Output
variable
JAVA_HOME
Linux
export JAVA_HOME=/usr/local/java-current
Mac
export JAVA_HOME=/Library/Java/Home
to
C:\Program
Output
GWT
Linux
export PATH=$PATH:$JAVA_HOME/bin/
Mac
not required
GWT
GWT
GWT
Further information about configuring and running Tomcat can be found in the
documentation included here, as well as on the Tomcat web site:
http://tomcat.apache.org
Tomcat can be stopped by executing the following commands on windows
machine:
%CATALINA_HOME%\bin\shutdown
Or C:\apache-tomcat-5.5.29\bin\shutdown
Tomcat can be stopped by executing the following commands on UNIX (Solaris,
Linux, etc.) machine:
$CATALINA_HOME/bin/shutdown.sh
Or
/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh
3. GWT APPLICATIONS
GWT
Before we start with creating actual HelloWorld application using GWT, let us
see what the actual parts of a GWT application are:
A GWT application consists of following four important parts out of which last
part is optional but first three parts are mandatory
Module descriptors
Public resources
Client-side code
Server-side code
Location
HelloWorld/
src/com/tutorialspoint/war/
Client-side code
src/com/tutorialspoint/client/
Server-side code
src/com/tutorialspoint/server/
Module Descriptors
A module descriptor is the configuration file in the form of XML which is used to
configure a GWT application.
A module descriptor file extension is *.gwt.xml, where * is the name of the
application and this file should reside in the project's root.
Following will be the default module descriptor HelloWorld.gwt.xml for a
HelloWorld application:
<?xml version="1.0" encoding="utf-8"?>
<module rename-to='helloworld'>
<!-- inherit the core web toolkit stuff.
-->
<inherits name='com.google.gwt.user.user'/>
-->
9
GWT
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='...'/>
<source path='...'/>
<!-- specify the paths for static files like html, css etc.
-->
<public path='...'/>
<public path='...'/>
-->
src="css-url" />
<stylesheet
src="css-url" />
-->
</module>
Following is a brief detail of different parts used in module descriptor
S.N.
1
GWT
5
Public Resources
These are the various files referenced by your GWT module, such as Host HTML
page, CSS or images.
The location of these resources can be configured using <public path="path" />
element in module configuration file. By default, it is the public subdirectory
underneath where the Module XML File is stored.
When you compile your application into JavaScript, all the files that can be
found on your public path are copied to the module's output directory.
The most important public resource is host page which is used to invoke actual
GWT application. A typical HTML host page for an application might not include
any visible HTML body content at all but it is always expected to include GWT
application via a <script.../> tag as follows
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>Hello World</h1>
<p>Welcome to first GWT application</p>
</body>
</html>
11
GWT
Following is the sample style sheet which we have included in our host page:
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;
}
Client-side Code
This is the actual Java code written for implementing the business logic of the
application and with this, GWT compiler translates into JavaScript, which will
eventually run inside the browser. The location of these resources can be
configured using <source path="path" /> element in module configuration file.
For example, Entry Point code will be used as client-side code and its location
will be specified using <source path="path" />.
A module entry-point is any class that is assignable to EntryPoint and that
can be constructed without parameters. When a module is loaded, every entry
point class is instantiated and its EntryPoint.onModuleLoad() method gets
called. A sample HelloWorld Entry Point class will be as follows:
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
Window.alert("Hello, World!");
}
}
Server-side Code
This is the server side part of your application and it is very much optional. If
you are not doing any backend processing within your application, then you do
not need this part. However, if there is some processing required at backend
and your client-side application interacts with the server, then you will have to
develop these components.
12
GWT
The next chapter will make use of all the above-mentioned concepts to create a
HelloWorld application using Eclipse IDE.
13
GWT
As the power of GWT lies in Write in Java, Run in JavaScript, we'll be using
Java IDE Eclipse to demonstrate our examples.
Let's start with a simple HelloWorld application:
14
GWT
Unselect Use Google App Engine because we're not using it in this project
and leave other default values (keep the Generate Sample project code
option checked as such and click Finish Button).
Once your project is created successfully, you will have the following content in
your Project Explorer:
Location
Source code (java classes) files.
Client folder containing the
responsible for client UI display.
src
client-side
specific
java
classes
GWT
test
war
Client folder containing the java classes responsible to test gwt client
side code.
This is the most important part, it represents the actual deployable
web application.
WEB-INF containing compiled classes, gwt libraries, servlet libraries.
HelloWorld.css, project style sheet.
HelloWorld.html, hots HTML which will invoke GWT UI Application.
-->
<inherits name='com.google.gwt.user.User'/>
-->
-->
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>
-->
-->
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
16
GWT
</module>
<h1>Hello World</h1>
<p>Welcome to first GWT application</p>
17
GWT
</body>
</html>
You can create more static files like HTML, CSS or images in the same source
directory or you can create further sub-directories and move files in those subdirectories and configure those sub-directories in module descriptor of the
application.
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
18
GWT
Keep the default values intact and click Compile button. If everything goes fine,
you will see following output in Eclipse console
Compiling module com.tutorialspoint.HelloWorld
Compiling 6 permutations
Compiling permutation 0...
Compiling permutation 1...
Compiling permutation 2...
Compiling permutation 3...
Compiling permutation 4...
Compiling permutation 5...
Compile of permutations succeeded
Linking into C:\workspace\HelloWorld\war\helloworld
Link succeeded
Compilation succeeded -- 33.029s
19
GWT
If everything is fine, you must see GWT Development Mode active in Eclipse
containing a URL as shown below. Double click the URL to open the GWT
application.
Because you are running your application in development mode, you will also
need to install GWT plugin for your browser. Simply follow the onscreen
instructions to install the plugin.
If you already have GWT plugin set for your browser, then you should be able
to see the following output
Congratulations! You have implemented your first application using Google Web
Toolkit (GWT).
20
GWT
21
GWT
This tutorial will explain you how to create an application war file and how to
deploy that in Apache Tomcat Websever root.
If you understood this simple example, then you will also be able to deploy a
complex GWT application following similar steps.
Now, let us have a working Eclipse IDE along with GWT plugin place and follow
certain steps to create a GWT application:
Step
Description
Compile and run the application to make sure business logic is working
as per the requirements.
Finally, zip the content of the war folder of the application in the form
of war file and deploy it in Apache Tomcat Webserver.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
22
GWT
-->
<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>Hello World</h1>
<div id="gwtContainer"></div>
</body>
23
GWT
</html>
I modified the HTML a little bit from the previous example. Here I created a
placeholder <div>...</div> where we will insert some content using our entry
point java class. So let us have the following content of Java file
src/com.tutorialspoint/HelloWorld.java.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
RootPanel.get("gwtContainer").add(html);
}
}
Here we created a basic widget HTML and added it inside the div tag having the
id="gwtContainer". We will study different GWT widgets in the coming
chapters.
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:
24
GWT
25
GWT
26
GWT
GWT widgets rely on cascading style sheets (CSS) for visual styling. By default,
the class name for each component is gwt-<classname>.
For example, the Button widget has a default style of gwt-Button and similar
way the TextBox widgest has a default style of gwt-TextBox.
In order to give all buttons and text boxes a larger font, you could put the
following rule in your application's CSS file
.gwt-Button
{ font-size: 150%; }
GWT
3
For example, let's define two new styles which we will apply to a text
.gwt-Big-Text{
font-size:150%;
}
.gwt-Small-Text{
font-size:75%;
}
.gwt-Red-Text{
color:red;
}
Now you can use setStyleName(Style) to change the default setting to new
setting. After applying the following rule, a text's font will become large
txtWidget.setStyleName("gwt-Big-Text");
We can apply a secondary CSS rule on the same widget to change its color as
follows:
txtWidget.addStyleName("gwt-Red-Text");
Using the above method, you can add as many styles as you like to apply on a
widget. If you remove the first style from the button widget, then the second
style will still remain with the text.
txtWidget.removeStyleName("gwt-Big-Text");
28
GWT
.BigText {
font-size: large;
}
.LoudText {
font-weight:
bold;
}
Let's suppose we want a particular label widget to always display blue text, and
in some cases, use a larger, bold font for added emphasis.
We could do something like this:
// set up our primary style
Label someText = new Label();
someText.setStylePrimaryName("MyText");
...
29
GWT
someText.removeStyleName("BigText");
someText.removeStyleName("LoudText");
Description
the
application
to
verify
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
the
result
module
of
the
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet.
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
30
GWT
-->
<entry-point class='com.tutorialspoint.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{
font-size: 150%;
font-weight: bold;
width:100px;
height:100px;
}
.gwt-Big-Text{
font-size:150%;
}
.gwt-Small-Text{
font-size:75%;
31
GWT
}
Following is the content of the modified HTML host file war/HelloWorld.html
to accommodate two buttons.
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
the
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will take care of adding two
buttons in HTML and will apply custom CSS style.
package com.tutorialspoint.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.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
32
GWT
// add button to change font to big when clicked.
Button Btn1 = new Button("Big Text");
Btn1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
RootPanel.get("mytext").setStyleName("gwt-Big-Text");
}
});
RootPanel.get("gwtGreenButton").add(Btn1);
RootPanel.get("gwtRedButton").add(Btn2);
}
}
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:
33
GWT
Now try clicking on the two buttons displayed and observe "Hello, World!" text
which keeps changing its font upon clicking on the two buttons.
34
GWT
UI elements : These are the core visual elements the user eventually
sees and interacts with. GWT provides a huge list of widely used and
common elements varying from basic to complex, which we will cover in
this tutorial.
Behavior: These are events which occur when the user interacts with UI
elements. This part will be covered in Event Handling chapter.
GWT UI Elements
The GWT library provides classes in a well-defined class hierarchy to create
complex web-based user interfaces. All classes in this component hierarchy has
been derived from the UIObject base class as shown below:
Every Basic UI widget inherits properties from a Widget class which in turn
inherits properties from UIObject. Tree and Menu will be covered in Complex
widgets tutorial later.
S.N.
1
widget
contains
text,
not
interpreted
as
HTML
using
a
35
GWT
<div>element, causing it to be displayed with block layout.
2
Class Declaration
Following
is
the
for com.google.gwt.user.client.ui.UIObject class:
declaration
Field
Following are the fields for com.google.gwt.user.client.ui.UIObject class:
Class Constructors
S.N.
1
36
GWT
Class Methods
S.N.
1
ensureDebugId(java.lang.String id)
Ensure that the main Element for this UIObject has an ID property set,
which allows it to integrate with third-party libraries and test tools.
int getAbsoluteLeft()
Gets the object's absolute left position in pixels, as measured from the
browser window's client area.
int getAbsoluteTop()
Gets the object's absolute top position in pixels, as measured from the
browser window's client area.
Element getElement()
Gets a handle to the object's underlying DOM element.
int getOffsetHeight()
Gets the object's offset height in pixels.
10
int getOffsetWidth()
37
GWT
Gets the object's offset width in pixels.
11
12
java.lang.String getStyleName()
Gets all of the object's style names, as a space-separated list.
13
14
java.lang.String getStylePrimaryName()
Gets the primary style name associated with the object.
15
16
java.lang.String getTitle()
Gets the title associated with this object.
17
boolean isVisible()
Determines whether or not this object is visible.
18
19
20
21
38
GWT
Removes a style name.
22
23
24
25
26
27
28
29
30
protected
static
void
java.lang.String style)
setStylePrimaryName(Element
elem,
Sets the element's primary style name and updates all dependent style
names.
31
GWT
names.
32
33
34
35
36
java.lang.String toString()
This method is overridden so that any object can be viewed in the
debugger as an HTML snippet.
37
Methods Inherited
This class inherits methods from the following classes:
java.lang.Object
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Widget class:
public class Widget
40
GWT
extends UIObject
implements EventListener
Field
Following are the fields for com.google.gwt.user.client.ui.Widget class:
Class Constructors
S.N.
1
Class Methods
S.N.
1
protected
<H
extends
EventHandler>
HandlerRegistration
addHandler(H handler, GwtEvent.Type<H> type)
Adds this handler to the widget.
GWT
If a widget implements HasWidgets, it must override this method and
call onDetach() for each of its child widgets.
6
Widget getParent()
Gets this widget's parent panel.
boolean isAttached()
Determines whether this widget is currently attached to the browser's
document (i.e., there is an unbroken chain of widgets between this
widget and the underlying browser document).
10
11
12
13
14
15
42
GWT
16
void removeFromParent()
Removes this widget from its parent widget.
17
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
Basic Widgets
Following are few important Basic Widgets:
S.N.
1
using
HTML
This widget can contain HTML text and displays the html content using a
<div> element, causing it to be displayed with block layout.
Image
This widget displays an image at a given URL.
Anchor
This widget represents a simple <a> element.
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Label class:
43
GWT
public class Label
extends Widget
implements HasHorizontalAlignment, HasText, HasWordWrap,
HasDirection, HasClickHandlers, SourcesClickEvents,
SourcesMouseEvents, HasAllMouseHandlers
Class Constructors
S.N.
Label()
Creates an empty label.
Label(java.lang.String text)
Creates a label with the specified text.
Class Methods
S.N.
1
44
GWT
2
HasDirection.Direction getDirection()
Gets the directionality of the widget.
HasHorizontalAlignment.HorizontalAlignmentConstant
getHorizontalAlignment()
Gets the horizontal alignment.
java.lang.String getText()
Gets this object's text.
boolean getWordWrap()
Gets whether word-wrapping is enabled.
10
11
12
45
GWT
13
void
setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentCo
nstant align)
Sets the horizontal alignment.
14
15
16
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
- Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
46
GWT
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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:
text-align: center;
}
.gwt-Label{
font-size:
150%;
font-weight: bold;
color:red;
47
GWT
padding:5px;
margin:5px;
}
.gwt-Green-Border{
border:1px solid green;
}
.gwt-Blue-Border{
border:1px solid blue;
}
Following
is
the
content
of
the
modified
file war/HelloWorld.html to accommodate two buttons.
HTML
host
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Label widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Label;
48
GWT
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
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:
49
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Label class:
public class HTML
extends Label
implements HasHTML
50
GWT
Class Constructors
S.N.
1
HTML(java.lang.String html)
Creates a HTML with the specified html contents.
Class Methods
1
java.lang.String getHTML()
Gets this object's contents as HTML.
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.Label
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.HasText
java.lang.Object
51
GWT
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
the
result
module
of
the
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
52
GWT
body{
text-align: center;
font-family: verdana, sans-serif;
}
h1{
font-size:
2em;
font-weight: bold;
color: #777777;
margin:
text-align: center;
}
.gwt-Green-Border{
border:1px solid green;
}
.gwt-Blue-Border{
border:1px solid blue;
}
</body>
53
GWT
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
HTML widget.
package com.tutorialspoint.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;
RootPanel.get("gwtContainer").add(panel);
}
}
54
GWT
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:
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Image class:
public class Image
extends Widget
implements SourcesLoadEvents, HasLoadHandlers,
HasErrorHandlers, SourcesClickEvents,
HasClickHandlers, HasAllMouseHandlers,
SourcesMouseEvents
55
GWT
Class Constructors
S.N.
1
Image(java.lang.String url)
Creates an image with the specified url.
Image(java.lang.String html, int left, int top, int width, int height)
Creates a clipped image with a specified URL and visibility rectangle.
Class Methods
S.N.
1
56
GWT
4
int getHeight()
Gets the height of the image.
int getOriginLeft()
Gets the horizontal co-ordinate of the upper-left vertex of the image's
visibility rectangle.
int getOriginTop()
Gets the vertical co-ordinate of the upper-left vertex of the image's
visibility rectangle.
java.lang.String getUrl()
Gets the URL of the image.
int getWidth()
Gets the width of the image.
10
11
12
13
14
57
GWT
15
16
17
18
19
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
58
GWT
3
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
59
GWT
color: #777777;
margin:
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>
</body>
</html>
Let
us
have
the
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate the use
of Image widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
Image image = new Image();
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:
61
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Anchor class:
public class Anchor
extends FocusWidget
implements HasHorizontalAlignment, HasName,
HasHTML, HasWordWrap, HasDirection
Class Constructors
S.N.
1
Anchor(java.lang.String text)
Creates an anchor for scripting.
GWT
Creates an anchor with its text and href (target URL) specified.
6
Class Methods
S.N.
1
HasHorizontalAlignment.HorizontalAlignmentConstant
getHorizontalAlignment()
Gets the horizontal alignment.
java.lang.String getHref()
Gets the anchor's href (the url to which it links).
java.lang.String getHTML()
Gets this object's contents as HTML.
java.lang.String getName()
Gets the widget's name.
int getTabIndex()
Gets the widget's position in the tab index.
63
GWT
7
java.lang.String getTarget()
Gets the anchor's target frame (the frame in which navigation will
occur when the link is selected).
java.lang.String getText()
Gets this object's text.
boolean getWordWrap()
Gets whether word-wrapping is enabled.
10
11
12
void
setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentC
onstant align)
Sets the horizontal alignment.
13
14
15
16
17
64
GWT
18
19
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
- Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
65
GWT
<!-- Inherit the default GWT style sheet.
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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:
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>
66
GWT
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Anchor widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
RootPanel.get("gwtContainer").add(panel);
}
}
67
GWT
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:
68
GWT
Form 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.
S.N.
1
using
Class Declaration
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:
69
GWT
Class Constructors
S.N.
1
Class Methods
S.N.
1
ensureDebugId(java.lang.String id)
Ensure that the main Element for this UIObject has an ID property set,
which allows it to integrate with third-party libraries and test tools.
int getAbsoluteLeft()
Gets the object's absolute left position in pixels, as measured from the
browser window's client area.
int getAbsoluteTop()
70
GWT
Gets the object's absolute top position in pixels, as measured from the
browser window's client area.
8
Element getElement()
Gets a handle to the object's underlying DOM element.
int getOffsetHeight()
Gets the object's offset height in pixels.
10
int getOffsetWidth()
Gets the object's offset width in pixels.
11
12
java.lang.String getStyleName()
Gets all of the object's style names, as a space-separated list.
13
14
java.lang.String getStylePrimaryName()
Gets the primary style name associated with the object.
15
16
java.lang.String getTitle()
Gets the title associated with this object.
17
boolean isVisible()
Determines whether or not this object is visible.
18
71
GWT
Determines whether element is visible or not.
19
20
21
22
23
24
25
26
27
28
72
GWT
29
30
protected
static
void
java.lang.String style)
setStylePrimaryName(Element
elem,
Sets the element's primary style name and updates all dependent style
names.
31
32
33
34
35
36
java.lang.String toString()
This method is overridden so that any object can be viewed in the
debugger as an HTML snippet.
37
Methods Inherited
This class inherits methods from the following classes:
java.lang.Object
73
GWT
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 Constructors
S.N.
1
Class Methods
S.N.
protected
<H
extends
EventHandler>
HandlerRegistration
addDomHandler(H handler, DomEvent.Type<H> type)
Adds a native event handler to the widget and sinks the corresponding
native event.
74
GWT
handler, GwtEvent.Type<H> type)
Adds this handler to the widget.
3
Widget getParent()
Gets this widget's parent panel.
boolean isAttached()
Determines whether this widget is currently attached to the browser's
document (i.e., there is an unbroken chain of widgets between this
widget and the underlying browser document).
10
11
12
GWT
13
14
15
16
void removeFromParent()
Removes this widget from its parent widget.
17
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
Form Widgets
Following are the few important Form Widgets:
S.N.
1
PushButton
This widget represents a normal push button with custom styling.
ToggleButton
This widget represents a stylish stateful button which allows the user to
toggle between up and down states.
CheckBox
This widget represents a standard check box widget. This class also
serves as a base class for RadioButton.
RadioButton
76
GWT
This widget represents a mutually-exclusive selection radio button
widget.
6
ListBox
This widget represents a list of choices to the user, either as a list box
or as a drop-down list.
SuggestBox
This widget represents a text box or text area which displays a preconfigured 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.
TextBox
This widget represents a single line text box.
PasswordTextBox
This widget represents a text box that visually masks its input to
prevent eavesdropping..
10
TextArea
This widget represents a text box that allows multiple lines of text to be
entered.
11
RichTextArea
This widget represents a rich text editor that allows complex styling and
formatting.
12
FileUpload
This widget wraps the HTML <input type='file'> element.
13
Hidden
This widget represets a hidden field in an HTML form.
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Button class:
77
GWT
public class Button
extends ButtonBase
Class Constructors
S.N.
1
Button(java.lang.String html)
Creates a button with the given HTML caption.
Class Methods
S.N.
1
78
GWT
Methods Inherited
This class inherits methods from the following classes:
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.ButtonBase
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
- Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
79
GWT
-->
<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>
80
GWT
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Button widget.
package com.tutorialspoint.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;
//create buttons
Button redButton = new Button("Red");
Button greenButton = new Button("Green");
Button blueButton = new Button("Blue");
GWT
redButton.setWidth("100px");
greenButton.setWidth("100px");
blueButton.setWidth("100px");
greenButton.addStyleName("gwt-Green-Button");
blueButton.addStyleName("gwt-Blue-Button");
GWT
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!
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.PushButton class:
83
GWT
public class PushButton
extends CustomButton
Class Constructors
S.N.
1
PushButton(Image upImage)
Creates a PushButton with up state image.
PushButton(java.lang.String upText)
Creates a PushButton with up state text.
84
GWT
7
PushButton(java.lang.String
ClickListener listener)
upText,
java.lang.String
downText,
Creates a PushButton with up state, down state text and click listener.
Class Methods
S.N.
1
Methods inherited
This class inherits methods from the following classes:
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.CustomWidget
java.lang.Object
GWT
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
86
GWT
}
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>
87
GWT
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
PushButton widget.
package com.tutorialspoint.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;
GWT
pushButton1.setEnabled(false);
89
GWT
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.
Class Declaration
Following
is
the
com.google.gwt.user.client.ui.ToggleButton class:
declaration
for
GWT
Class Constructors
S.N.
1
ToggleButton(Image upImage)
Creates a ToggleButton with up state image.
ToggleButton(Image
listener)
upImage,
Image
downImage,
ClickListener
ToggleButton(java.lang.String upText)
Creates a ToggleButton with up state text.
91
GWT
8
ToggleButton(java.lang.String
ClickListener listener)
upText,
java.lang.String
downText,
Class Methods
S.N.
boolean isDown()
Is this button down?
Methods Inherited
This class inherits methods from the following classes:
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.CustomWidget
java.lang.Object
Description
92
GWT
1
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
93
GWT
}
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">
94
GWT
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
ToggleButton widget.
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
GWT
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.
96
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.CheckBox class:
public class CheckBox
extends ButtonBase
implements HasName
Class Constructors
S.N.
1
CheckBox(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
CheckBox(java.lang.String label)
Creates a check box with the specified text label.
97
GWT
Class Methods
S.N.
1
int getTabIndex()
Gets the widget's position in the tab index.
java.lang.String getText()
Gets this object's text.
boolean isChecked()
Determines whether this check box is currently checked.
boolean isEnabled()
Gets whether this widget is enabled.
10
98
GWT
11
12
13
14
15
16
17
18
19
java.lang.String getHTML()
Gets this object's contents as HTML.
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
java.lang.Object
99
GWT
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
the
result
module
of
the
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
100
GWT
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>
</body>
</html>
101
GWT
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
CheckBox widget.
package com.tutorialspoint.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;
//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");
}
});
102
GWT
// 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:
show
an
alert
message
103
GWT
selection radio
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.RadioButton class:
public class RadioButton
extends CheckBox
Class Constructors
S.N.
1
RadioButton(java.lang.String
asHTML)
name,java.lang.String
label,
boolean
Class Methods
S.N.
104
GWT
1
Methods Inherited
This class inherits methods from the following classes:
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.CheckBox
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
the
result
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
of
the
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
105
GWT
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
106
GWT
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
RadioButton widget.
package com.tutorialspoint.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;
GWT
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
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.ListBox class:
public class ListBox
extends FocusWidget
implements SourcesChangeEvents, HasName
Class Constructors
S.N.
1
ListBox(boolean isMultipleSelect)
Creates an empty list box.
ListBox(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
Class Methods
S.N.
1
109
GWT
3
void clear()
Removes all items from the list box.
int getItemCount()
Gets the number of items present in the list box.
java.lang.String getName()
Gets the widget's name.
int getSelectedIndex()
Gets the currently-selected item.
int getVisibleItemCount()
Gets the number of items that are visible.
10
11
void insertItem(java.lang.String
index)
item,
java.lang.String
value,
int
Inserts an item into the list box, specifying an initial value for the item.
12
13
boolean isMultipleSelect()
Gets whether this list allows multiple selection.
14
110
GWT
Fired whenever a browser event is received.
15
16
17
18
19>
20
21
22
23
24
25
111
GWT
26
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
java.lang.Object
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
112
GWT
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
GWT
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
ListBox widget.
package com.tutorialspoint.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;
114
GWT
// 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");
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:
115
GWT
Class Declaration
Following
is
the
com.google.gwt.user.client.ui.SuggestionBox class:
declaration
for
GWT
extends Composite
implements HasText, HasFocus, HasAnimation,
SourcesClickEvents, SourcesFocusEvents,
SourcesChangeEvents, SourcesKeyboardEvents,
FiresSuggestionEvents
117
GWT
Class Constructors
S.N.
1
SuggestBox(SuggestOracle oracle)
Constructor for Suggestion Box.
Class Methods
S.N.
1
int getLimit()
Gets the limit for the number of suggestions that should be displayed
for this box.
118
GWT
7
SuggestOracle getSuggestOracle()
Gets the suggest box's SuggestOracle.
int getTabIndex()
Gets the widget's position in the tab index.
java.lang.String getText()
Gets this object's text.
10
boolean isAnimationEnabled()
Gets whether animation is enabled or not.
11
12
13
14
15
16
17
119
GWT
18
19
20
21
22
23
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Composite
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
120
GWT
2
the
application
to
verify
the
result
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
of
the
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
121
GWT
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;
}
122
GWT
.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 {
123
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
SuggestionBox widget.
package com.tutorialspoint.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;
GWT
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);
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:
125
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.TextBox class:
public class TextBox
extends TextBoxBase
implements HasDirection
126
GWT
Class Constructors
S.N.
1
TextBox(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
Class methods
S.N.
1
int getMaxLength()
Gets the maximum allowable length of the text box.
int getVisibleLength()
Gets the number of visible characters in the text box.
127
GWT
6
Methods Inherited
This class inherits methods from the following classes:
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
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
128
GWT
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
129
GWT
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
TextBox widget.
package com.tutorialspoint.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;
GWT
textBox2.setReadOnly(true);
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
Class Declaration
Following
is
the
declaration
com.google.gwt.user.client.ui.PasswordTextBox class:
for
Class Constructors
S.N.
1
PasswordTextBox(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
Class Methods
S.N.
1
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
132
GWT
com.google.gwt.user.client.ui.FocusWidget
com.google.gwt.user.client.ui.TextBoxBase
com.google.gwt.user.client.ui.TextBox
java.lang.Object
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
133
GWT
<source path='client'/>
<source path='shared'/>
</module>
134
GWT
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
PasswordTextBox widget.
package com.tutorialspoint.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;
GWT
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:
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.TextArea
class:
public class TextArea
136
GWT
extends TextBoxBase
implements HasDirection
{}
.gwt-TextArea-readonly {}
Class Constructors
S.N.
1
Class Methods
S.N.
1
int getCursorPos()
Gets the current position of the cursor (this also serves as the
beginning of the text selection).
HasDirection.Direction getDirection()
Gets the directionality of the widget.
int getSelectionLength()
Gets the length of the current text selection.
int getVisibleLines()
137
GWT
Gets the number of text lines that are visible.
6
Methods Inherited
This class inherits methods from the following classes:
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
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
138
GWT
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
}
139
GWT
.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>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
TextBox widget.
package com.tutorialspoint.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;
GWT
TextArea textArea1 = new TextArea();
TextArea textArea2 = new TextArea();
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:
141
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.RichTextArea
class:
public class RichTextArea
extends FocusWidget
implements HasHTML, HasInitializeHandlers, HasSafeHtml
142
GWT
Class Constructors
S.N.
1
Class Methods
S.N.
1
RichTextArea.BasicFormatter getBasicFormatter()
Deprecated. use getFormatter() instead.
RichTextArea.ExtendedFormatter getExtendedFormatter()
Deprecated. use getFormatter() instead.
RichTextArea.Formatter getFormatter()
Gets the rich text formatting interface.
java.lang.String getHTML()
Gets this object's contents as HTML.
java.lang.String getText()
Gets this object's text.
boolean isEnabled()
143
GWT
Gets whether this widget is enabled.
8
10
11
12
13
14
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
java.lang.Object
GWT
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
GWT
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;
}
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
TextBox widget.
146
GWT
package com.tutorialspoint.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;
richTextArea.setHeight("200");
richTextArea.setWidth("200");
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:
147
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.FileUpload class:
public class FileUpload
extends Widget
implements HasName, HasChangeHandlers
GWT
.gwt-FileUpload {}
Class Constructors
S.No
1
FileUpload(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
Class Methods
S.No
1
java.lang.String getFilename()
Gets the filename selected by the user.
java.lang.String getName()
Gets the widget's name.
boolean isEnabled()
Gets whether this widget is enabled.
GWT
Sets the widget's name.
8
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
the
result
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
of
the
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
150
GWT
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
151
GWT
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
FileUpload widget.
package com.tutorialspoint.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;
GWT
//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.tutorialspoint.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
153
GWT
//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);
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:
154
GWT
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"%>
155
GWT
<%@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);
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
156
GWT
+",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());
}
%>
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Hidden class:
157
GWT
Class Constructors
S.N.
1
Hidden(Element element)
This constructor may be used by subclasses to explicitly use an
existing element.
Hidden(java.lang.String name)
Constructor for Hidden.
Class Methods
S.N.
1
java.lang.String getID()
Gets the id of the hidden field.
java.lang.String getName()
Gets the name of the hidden field.
java.lang.String getValue()
Gets the value of the hidden field.
158
GWT
6
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
159
GWT
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Hidden widget.
package com.tutorialspoint.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;
GWT
//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());
}
});
RootPanel.get("gwtContainer").add(panel);
}
}
162
GWT
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:
163
GWT
using
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.UIObject
class:
GWT
Field
Following are the fields for com.google.gwt.user.client.ui.UIObject class:
Class Constructors
S.N.
UIObject()
This creates a UIObject for the child classes.
Class Methods
S.N.
ensureDebugId(java.lang.String id)
Ensure that the main Element for this UIObject has an ID property set,
which allows it to integrate with third-party libraries and test tools.
int getAbsoluteLeft()
Gets the object's absolute left position in pixels, as measured from the
browser window's client area.
165
GWT
7
int getAbsoluteTop()
Gets the object's absolute top position in pixels, as measured from the
browser window's client area.
Element getElement()
Gets a handle to the object's underlying DOM element.
int getOffsetHeight()
Gets the object's offset height in pixels.
10
int getOffsetWidth()
Gets the object's offset width in pixels.
11
12
java.lang.String getStyleName()
Gets all of the object's style names, as a space-separated list.
13
14
java.lang.String getStylePrimaryName()
Gets the primary style name associated with the object.
15
16
java.lang.String getTitle()
Gets the title associated with this object.
17
boolean isVisible()
Determines whether or not this object is visible.
166
GWT
18
19
20
21
22
23
24
25
26
27
28
167
GWT
This convenience method adds or removes a style name for a given
element.
29
30
protected
static
void
java.lang.String style)
setStylePrimaryName(Element
elem,
Sets the element's primary style name and updates all dependent style
names.
31
32
33
34
35
36
java.lang.String toString()
This method is overridden so that any object can be viewed in the
debugger as an HTML snippet.
37
Methods inherited
This class inherits methods from the following classes:
168
GWT
java.lang.Object
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 Constructors
S.N.
1
Class Methods
S.N.
1
GWT
2
protected
<H
extends
EventHandler>
addHandler(H handler, GwtEvent.Type<H> type)
HandlerRegistration
Widget getParent()
Gets this widget's parent panel.
boolean isAttached()
Determines whether this widget is currently attached to the browser's
document (i.e., there is an unbroken chain of widgets between this
widget and the underlying browser document).
10
11
12
GWT
Fired whenever a browser event is received.
13
14
15
16
void removeFromParent()
Removes this widget from its parent widget.
17
Methods inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
Complex Widgets
Following are few important Complex Widgets:
S.N.
Tree
This widget represents a standard hierarchical tree widget. The tree
contains a hierarchy of TreeItems that the user can open, close, and
select.
MenuBar
This widget represents a standard menu bar widget. A menu bar can
contain any number of menu items, each of which can either fire a
171
GWT
Command or open a cascaded menu bar.
DatePicker
This widget represents a standard GWT date picker.
CellTree
This widget represents a view of a tree. This widget will only work in
standards mode, which requires that the HTML page in which it is run
have an explicit <!DOCTYPE> declaration.
CellList
This widget represents a single column list of cells.
CellTable
This widget represents a tabular view that supports paging and
columns.
CellBrowser
This widget represents a browsable view of a tree in which only a
single node per level may be open at one time. This widget will only
work in standards mode, which requires that the HTML page in which it
is run have an explicit <!DOCTYPE> declaration.
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Tree class:
public class Tree
extends Widget
172
GWT
implements HasWidgets, SourcesTreeEvents, HasFocus,
HasAnimation, HasAllKeyHandlers, HasAllFocusHandlers,
HasSelectionHandlers, HasOpenHandlers,
HasCloseHandlers, SourcesMouseEvents,
HasAllMouseHandlers
Class Constructors
S.N.
1
Tree(TreeImages images)
Constructs a tree that uses the specified image bundle for images.
Class methods
S.N.
1
GWT
3
void clear()
Clears all tree items from the current tree.
10
11
12
void ensureSelectedItemVisible()
Ensures that the currently-selected item is visible, opening its parents
and scrolling the tree as necessary.
13
java.lang.String getImageBase()
Deprecated. Use Tree(TreeImages) as it provides a more efficent and
manageable way to supply a set of images to be used within a tree.
174
GWT
14
15
int getItemCount()
Gets the number of items contained at the root of this tree.
16
TreeItem getSelectedItem()
Gets the currently selected item.
17
int getTabIndex()
Gets the widget's position in the tab index.
18
boolean isAnimationEnabled()
19
protected
currentItem)
boolean
isKeyboardNavigationEnabled(TreeItem
Indicates if keyboard navigation is enabled for the Tree and for a given
TreeItem.
20
java.util.Iterator<Widget> iterator()
Gets an iterator for the contained widgets.
21
22
23
24
boolean remove(Widget w)
Removes a child widget.
25
GWT
Removes a previously added listener interface.
26
27
void removeItems()
Removes all items from the root level of this tree.
28
29
30
31
32
33
34
35
36
176
GWT
37
java.util.Iterator<TreeItem> treeItemIterator()
Iterator of tree items.
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
177
GWT
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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-Label {
font-weight: bold;
color: maroon;
}
.gwt-Tree .gwt-TreeItem {
padding: 1px 0px;
margin: 0px;
white-space: nowrap;
cursor: hand;
cursor: pointer;
178
GWT
}
.gwt-Tree .gwt-TreeItem-selected {
background: #ebeff9;
}
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of Tree
widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
179
GWT
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
tree.addSelectionHandler(new SelectionHandlerL<TreeItem>() {
@Override
public void onSelection(SelectionEvent<TreeItem> event) {
labelMessage.setText("Selected Value: "
+ event.getSelectedItem().getText());
}
});
181
GWT
//add the tree to the root panel
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:
Selecting any value in tree, will update a message below the tree displaying the
selected value.
182
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.MenuBar
class:
public class MenuBar
extends Widget
implements PopupListener, HasAnimation,
HasCloseHandlers<PopupPanel>
GWT
.gwt-MenuBarPopup .menuPopupMiddleRight {}
.gwt-MenuBarPopup .menuPopupMiddleRightInner {}
.gwt-MenuBarPopup .menuPopupBottomLeft {}
.gwt-MenuBarPopup .menuPopupBottomLeftInner {}
.gwt-MenuBarPopup .menuPopupBottomCenter {}
.gwt-MenuBarPopup .menuPopupBottomCenterInner {}
.gwt-MenuBarPopup .menuPopupBottomRight {}
.gwt-MenuBarPopup .menuPopupBottomRightInner {}
Class Constructors
S.N.
1
MenuBar(boolean vertical)
Creates an empty menu bar.
MenuBar(MenuBar.MenuBarImages images)
Deprecated. replaced by MenuBar(Resources)
MenuBar(MenuBar.Resources resources)
Creates an empty horizontal menu bar that uses the specified
ClientBundle for menu images.
184
GWT
Class Methods
S.N.
1
MenuItemSeparator addSeparator()
Adds a thin line to the MenuBar to separate sections of MenuItems.
185
GWT
10
11
void clearItems()
Removes all menu items from this menu bar.
12
13
void focus()
Give this MenuBar focus.
14
boolean getAutoOpen()
Gets whether this menu bar's child menus will open when the mouse is
moved over it.
15
16
17
18
19
20
186
GWT
21
22
boolean isAnimationEnabled()
Returns true if animations are enabled, false if not.
23
boolean isFocusOnHoverEnabled()
Check whether or not this widget will steal keyboard focus when the
mouse hovers over it.
24
void moveSelectionDown()
Moves the menu selection down to the next item.
25
void moveSelectionUp()
Moves the menu selection up to the previous item.
26
27
28
29
30
31
GWT
32
33
34
35
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
188
GWT
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
189
GWT
.gwt-MenuBar {
cursor: default;
}
.gwt-MenuBar .gwt-MenuItem {
cursor: default;
font-family: Arial Unicode MS, Arial, sans-serif;
font-size: 12px;
}
.gwt-MenuBar .gwt-MenuItem-selected {
background: #E3E8F3;
}
.gwt-MenuBar-horizontal {
background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px;
border: 1px solid #e0e0e0;
}
.gwt-MenuBar-horizontal .gwt-MenuItem {
padding: 5px 10px;
vertical-align: bottom;
color: #000;
font-weight: bold;
}
.gwt-MenuBar-horizontal .gwt-MenuItemSeparator {
width: 1px;
padding: 0px;
margin: 0px;
border: 0px;
border-left: 1px solid #ccc;
background: white;
}
.gwt-MenuBar-horizontal .gwt-MenuItemSeparator .menuSeparatorInner {
width: 1px;
height: 1px;
background: white;
}
190
GWT
.gwt-MenuBar-vertical {
margin-top: 0px;
margin-left: 0px;
background: white;
}
.gwt-MenuBar-vertical table {
border-collapse: collapse;
}
.gwt-MenuBar-vertical .gwt-MenuItem {
padding: 2px 40px 2px 1px;
}
.gwt-MenuBar-vertical .gwt-MenuItemSeparator {
padding: 2px 0px;
}
.gwt-MenuBar-vertical .gwt-MenuItemSeparator .menuSeparatorInner {
height: 1px;
padding: 0px;
border: 0px;
border-top: 1px solid #ccc;
overflow: hidden;
}
.gwt-MenuBar-vertical .subMenuIcon {
padding-right: 4px;
}
.gwt-MenuBar-vertical .subMenuIcon-selected {
background: #E3E8F3;
}
.gwt-MenuBarPopup {
margin: 0px 0px 0px 3px;
}
.gwt-MenuBarPopup .menuPopupTopLeftInner {
width: 5px;
height: 5px;
191
GWT
zoom: 1;
}
.gwt-MenuBarPopup .menuPopupTopRightInner {
width: 8px;
height: 5px;
zoom: 1;
}
.gwt-MenuBarPopup .menuPopupBottomLeftInner {
width: 5px;
height: 8px;
zoom: 1;
}
.gwt-MenuBarPopup .menuPopupBottomRightInner {
width: 8px;
height: 8px;
zoom: 1;
}
.gwt-MenuBarPopup .menuPopupTopLeft {
background: url(images/corner.png) no-repeat 0px -36px;
-background: url(images/corner_ie6.png) no-repeat 0px -36px;
}
.gwt-MenuBarPopup .menuPopupTopRight {
background: url(images/corner.png) no-repeat -5px -36px;
-background: url(images/corner_ie6.png) no-repeat -5px -36px;
}
.gwt-MenuBarPopup .menuPopupBottomLeft {
background: url(images/corner.png) no-repeat 0px -41px;
-background: url(images/corner_ie6.png) no-repeat 0px -41px;
}
.gwt-MenuBarPopup .menuPopupBottomRight {
background: url(images/corner.png) no-repeat -5px -41px;
-background: url(images/corner_ie6.png) no-repeat -5px -41px;
}
html>body .gwt-MenuBarPopup {
192
GWT
}
* html .gwt-MenuBarPopup .menuPopupTopLeftInner {
width: 5px;
height: 5px;
overflow: hidden;
}
* html .gwt-MenuBarPopup .menuPopupTopRightInner {
width: 8px;
height: 5px;
overflow: hidden;
}
* html .gwt-MenuBarPopup .menuPopupBottomLeftInner {
width: 5px;
height: 8px;
overflow: hidden;
}
* html .gwt-MenuBarPopup .menuPopupBottomRightInner {
width: 8px;
height: 8px;
overflow: hidden;
}
193
GWT
<h1>MenuBar Widget Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
MenuBar widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
GWT
GWT
});
editMenu.addItem("Redo", new Command() {
@Override
public void execute() {
showSelectedMenuItem("Redo");
}
});
editMenu.addItem("Cut", new Command() {
@Override
public void execute() {
showSelectedMenuItem("Cut");
}
});
editMenu.addItem("Copy", new Command() {
@Override
public void execute() {
showSelectedMenuItem("Copy");
}
});
editMenu.addItem("Paste", new Command() {
@Override
public void execute() {
showSelectedMenuItem("Paste");
}
});
GWT
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:
Selecting any value in menubar, will popup an alert message showing the
selected value.
197
GWT
Class Declaration
Following
is
the
declaration
com.google.gwt.user.datepicker.client.DatePicker class:
for
.datePickerPreviousButton {
.datePickerNextButton {
.datePickerDays {
.datePickerWeekdayLabel {
.datePickerWeekendLabel {
.datePickerDay {
.datePickerDayIsToday {
.datePickerDayIsWeekend { }
.datePickerDayIsFiller {
.datePickerDayIsValue {
}
}
.datePickerDayIsDisabled { }
.datePickerDayIsHighlighted {
.datePickerDayIsValueAndHighlighted { }
198
GWT
Class Constructors
S.N.
1
protected DatePicker(MonthSelector
view, CalendarModel model)
monthSelector,
CalendarView
Class Methods
S.N.
1
Handler
Registration
add
Show
(ShowRangeHandler<java.util.Date> handler)
Range
Handler
void
add
Style
To
Dates(java.lang.String
java.lang.Iterable<java.util.Date> dates)
style
Name,
199
GWT
Add a style name to the given dates.
7
void
addTransientStyleToDates(java.lang.String
java.util.Date date)
styleName,
Adds the given style name to the specified dates, which must be
visible.
8
void
addTransientStyleToDates(java.lang.String
java.util.Date date, java.util.Date... moreDates)
styleName,
Adds the given style name to the specified dates, which must be
visible.
9
styleName,
Adds the given style name to the specified dates, which must be
visible.
10
Handler
Registration
addValue
Handler(ValueChangeHandler<java.util.Date> handler)
Change
java.util.Date getCurrentMonth()
Gets the current month the date picker is showing.
12
java.util.Date getFirstDate()
Returns the first shown date.
13
java.util.Date getHighlightedDate()
Gets the highlighted date (the one the mouse is hovering over), if any.
14
java.util.Date getLastDate()
Returns the last shown date.
15
16
GWT
Gets the MonthSelector associated with this date picker.
17
18
java.util.Date getValue()
Returns the selected date, or null if none is selected.
19
20
21
22
void onLoad()
This method is called immediately after a widget becomes attached to
the browser's document.
23
24
25
26
void
remove
Style
From
Dates(java.lang.String
java.lang.Iterable<java.util.Date> dates)
styleName,
201
GWT
27
28
29
void setTransientEnabledOnDates(boolean
date)
enabled,
java.util.Date
31
void
set
Transient
Enabled
On
java.lang.Iterable<java.util.Date> dates)
Dates(boolean
enabled,
33
34
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Composite
java.lang.Object
202
GWT
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
203
GWT
</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:
text-align: center;
}
.gwt-DatePicker {
border:
1px
solid #ccc;
GWT
font-size: 85%;
text-align: center;
padding: 4px;
outline: none;
font-weight:bold;
color:#333;
border-right: 1px solid #EDEDED;
border-bottom: 1px solid #EDEDED;
}
.datePickerWeekdayLabel,
.datePickerWeekendLabel {
background: #fff;
padding: 0px 4px 2px;
cursor: default;
color:#666;
font-size:70%;
font-weight:normal;
}
.datePickerDay {
padding: 4px 7px;
cursor: hand;
cursor: pointer;
}
.datePickerDayIsWeekend {
background: #f7f7f7;
}
.datePickerDayIsFiller {
color: #999;
font-weight:normal;
}
.datePickerDayIsValue {
background: #d7dfe8;
}
.datePickerDayIsDisabled {
205
GWT
color: #AAAAAA;
font-style: italic;
}
.datePickerDayIsHighlighted {
background: #F0E68C;
}
.datePickerDayIsValueAndHighlighted {
background: #d7dfe8;
}
.datePickerDayIsToday {
padding: 3px;
color: #00f;
background: url(images/hborder.png) repeat-x 0px -2607px;
}
.datePickerMonthSelector {
width:
100%;
GWT
font-size: 100%;
font-weight: bold;
color: #333;
}
.gwt-DateBox {
padding: 5px 4px;
border: 1px solid #ccc;
border-top: 1px solid #999;
font-size: 100%;
}
.gwt-DateBox input {
width: 8em;
}
.dateBoxFormatError {
background: #ffcccc;
}
.dateBoxPopup {
}
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>
</body>
207
GWT
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of Tree
widget.
package com.tutorialspoint.client;
import java.util.Date;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
// Set the value in the text box when the user selects a date
datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
@Override
public void onValueChange(ValueChangeEvent<Date> event) {
Date date = event.getValue();
String dateString =
DateTimeFormat.getFormat("MM/dd/yyyy").format(date);
text.setText(dateString);
}
208
GWT
});
// Set the default value
datePicker.setValue(new Date(), true);
// Create a DateBox
DateTimeFormat dateFormat =
DateTimeFormat.getFormat("MM/dd/yyyy");
DateBox dateBox = new DateBox();
dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
RootPanel.get("gwtContainer").add(vPanel);
}
}
Once you are 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 will produce the following result:
209
GWT
210
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.cellview.client.CellTree
class:
public class CellTree
extends AbstractCellTree
implements HasAnimation, Focusable
Class Constructors
S.N.
1
Class Methods
S.N.
1
CellTree.NodeAnimation getAnimation()
Get the animation used to open and close nodes in this tree if
animations are enabled.
int getDefaultNodeSize()
Get the default maximum number of children to display under each
tree node.
TreeNode getRootTreeNode()
Get the root TreeNode.
211
GWT
5
int getTabIndex()
Gets the widget's position in the tab index.
boolean isAnimationEnabled()
Returns true if animations are enabled, false if not.
10
11
12
13
14
15
Methods Inherited
This class inherits methods from the following classes:
212
GWT
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Composite
com.google.gwt.user.cellview.client.AbstractCellTree
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
213
GWT
<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;
}
</body>
</html>
214
GWT
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
CellTree widget.
package com.tutorialspoint.client;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.
HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TreeNode;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
/**
* A list of songs.
*/
private static class Playlist {
private final String name;
private final List<String> songs = new ArrayList<String>();
215
GWT
public Playlist(String name) {
this.name = name;
}
/**
* Add a song to the playlist.
*
* @param name the name of the song
*/
public void addSong(String name) {
songs.add(name);
}
/**
* Return the list of songs in the playlist.
*/
public List<String> getSongs() {
return songs;
}
}
/**
* A composer of classical music.
*/
private static class Composer {
private final String name;
private final List<Playlist> playlists = new
ArrayList<Playlist>();
GWT
this.name = name;
}
/**
* Add a playlist to the composer.
*
* @param playlist the playlist to add
*/
public Playlist addPlaylist(Playlist playlist) {
playlists.add(playlist);
return playlist;
}
/**
* Return the rockin' playlist for this composer.
*/
public List<Playlist> getPlaylists() {
return playlists;
}
}
/**
* The model that defines the nodes in the tree.
*/
private static class CustomTreeModel implements TreeViewModel {
/**
* This selection model is shared across all leaf nodes.
217
GWT
* A selection model can also be shared across all nodes
* in the tree, or each set of child nodes can have
* its own instance. This gives you flexibility to
* determine how nodes are selected.
*/
private final SingleSelectionModel<String> selectionModel
= new SingleSelectionModel<String>();
public CustomTreeModel() {
// Create a database of information.
composers = new ArrayList<Composer>();
GWT
sonatas.addSong("Sonata in F Major");
GWT
new Playlist("Sonatas"));
sonatas.addSong("Two Sonatas for Clarinet - F Minor");
sonatas.addSong("Two Sonatas for Clarinet - E-Flat Major");
/**
* Get the {@link NodeInfo} that provides the children of the
* specified value.
*/
public <T> NodeInfo<?> getNodeInfo(T value) {
if (value == null) {
// LEVEL 0.
// We passed null as the root value. Return the composers.
220
GWT
");
sb.appendEscaped(value.getName());
}
}
};
// Return a node info that pairs the data provider and the
cell.
return new DefaultNodeInfo<Composer>(dataProvider, cell);
} else if (value instanceof Composer) {
// LEVEL 1.
// We want the children of the composer. Return the
playlists.
ListDataProvider<HelloWorld.Playlist> dataProvider
= new ListDataProvider<HelloWorld.Playlist>(
((Composer) value).getPlaylists());
Cell<HelloWorld.Playlist> cell =
new AbstractCell<HelloWorld.Playlist>() {
@Override
public void render(Playlist value, Object key,
SafeHtmlBuilder sb) {
221
GWT
if (value != null) {
sb.appendHtmlConstant("
");
sb.appendEscaped(value.getName());
}
}
};
return new DefaultNodeInfo<Playlist>(dataProvider, cell);
} else if (value instanceof Playlist) {
// LEVEL 2 - LEAF.
// We want the children of the playlist. Return the songs.
ListDataProvider<String> dataProvider
= new ListDataProvider<String>(
((Playlist) value).getSongs());
return null;
}
/**
* Check if the specified value represents a leaf node.
* Leaf nodes cannot be opened.
*/
public boolean isLeaf(Object value) {
// The leaf nodes are the songs, which are Strings.
if (value instanceof String) {
return true;
}
return false;
}
222
GWT
}
tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
// Open the first playlist by default.
TreeNode rootNode = tree.getRootTreeNode();
TreeNode firstPlaylist = rootNode.setChildOpen(0, true);
firstPlaylist.setChildOpen(0, true);
223
GWT
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.cellview.client.CellList<T>
class:
public class CellList<T>
extends AbstractHasData<T>
Class Constructors
S.N.
1
Class Methods
S.N.
1
Cell
Preview
Event.Handler)
225
GWT
instead.
3
SafeHtml getEmptyListMessage()
Get the message that is displayed when there is no data.
10
11
12
13
226
GWT
Called when the widget is focused.
14
15
16
17
index,
boolean
selected,
19
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.cellview.client.AbstractHasData
java.lang.Object
Description
227
GWT
1
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
the
result
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
of
the
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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{
228
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
CellList widget.
package com.tutorialspoint.client;
import java.util.Arrays;
import java.util.List;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.EntryPoint;
229
GWT
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;
/**
* A custom {@link Cell} used to render a {@link Contact}.
*/
private static class ContactCell extends AbstractCell<Contact> {
@Override
public void render(Contact value, Object key, SafeHtmlBuilder sb)
{
if (value != null) {
sb.appendEscaped(value.name);
}
230
GWT
}
}
/**
* The list of data to display.
*/
private static final List<Contact> CONTACTS = Arrays.asList(new
Contact(
"John"), new Contact("Joe"), new Contact("Michael"),
new Contact("Sarah"), new Contact("George"));
GWT
= new SingleSelectionModel<Contact>(
keyProvider);
cellList.setSelectionModel(selectionModel);
/*
* Select a contact. The selectionModel will select based on the
* ID because we used a keyProvider.
*/
Contact sarah = CONTACTS.get(3);
selectionModel.setSelected(sarah, true);
/*
* Redraw the CellList. Sarah/Sara will still be selected because we
* identify her by ID. If we did not use a keyProvider,
* Sara would not be selected.
*/
cellList.redraw();
232
GWT
Class Declaration
Following
is
the
declaration
com.google.gwt.user.cellview.client.CellTable<T> class:
for
Class Constructors
S.N.
1
233
GWT
Constructs a table with a default page size of 15.
2
CellTable(int pageSize)
Constructs a table with the given page size.
CellTable(int
pageSize,
ProvidesKey<T> keyProvider)
CellTable.Resources
resources,
the
specified
CellTable(ProvidesKey<T> keyProvider)
Constructs a table with a default page size of 15, and the given key
provider.
Class Methods
S.N.
1
234
GWT
Adds a column to the table with an associated SafeHtml header.
5
void add Column (Column<T,?> col, java. lang. String header String,
java.lang.String footer String)
Adds a column to the table with an associated String header and
footer.
10
11
protected void do Selection (Event event, T value, int row, int column)
Deprecated.
use
Abstract
Has
Data.add
Handler(com.google.gwt.view.client. Cell Preview
instead.
12
Cell
Preview
Event.Handler)
int getBodyHeight()
Return the height of the table body.
13
235
GWT
14
int getHeaderHeight()
Return the height of the table header.
15
16
17
18
19
20
21
void redrawFooters()
Redraw the table's footers.
22
void redrawHeaders()
Redraw the table's headers.
23
24
25
236
GWT
Remove a style from the TableColElement at the specified index.
26
27
28
29
index,
boolean
selected,
31
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.cellview.client.AbstractHasData
java.lang.Object
GWT
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
src/com.tutorialspoint/HelloWorld.gwt.xml.
modified
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
238
GWT
}
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
CellTable widget.
package com.tutorialspoint.client;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
239
GWT
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client
.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
/**
* The list of data to display.
*/
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("John", new Date(80,
4, 12),
GWT
new Contact("Joe", new Date(85,
2, 22),
6, 6),"1600
Pennsylvania
GWT
@Override
public String getValue(Contact object) {
return object.address;
}
};
table.addColumn(addressColumn, "Address");
242
GWT
// Add the widgets to the root panel.
RootPanel.get().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:
243
GWT
Class Declaration
Following
is
the
declaration
com.google.gwt.user.cellview.client.CellBrowser<T> class:
for
Class Constructors
S.N.
CellBrowser(TreeViewModel
viewModel,
CellBrowser.Resources resources)
rootValue,
Class Methods
S.N.
1
int getDefaultColumnWidth()
Get the default width of new columns.
int getMinimumColumnWidth()
Get the minimum width of columns.
TreeNode getRootTreeNode()
Get the root TreeNode.
boolean isAnimationEnabled()
244
GWT
Returns true if animations are enabled, false if not.
6
void onResize()
This method must be called whenever the implementor's size has been
modified.
10
(Has
Keyboard
Selection
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Composite
com.google.gwt.user.cellview.client.AbstractCellTree
java.lang.Object
245
GWT
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
GWT
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>
</body>
</html>
import java.util.ArrayList;
247
GWT
import java.util.List;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellBrowser;
import com.google.gwt.user.cellview.client.
HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
/**
* A list of songs.
*/
private static class Playlist {
private final String name;
private final List<String> songs = new ArrayList<String>();
/**
* Add a song to the playlist.
*
* @param name the name of the song
*/
248
GWT
public void addSong(String name) {
songs.add(name);
}
/**
* Return the list of songs in the playlist.
*/
public List<String> getSongs() {
return songs;
}
}
/**
* A composer of classical music.
*/
private static class Composer {
private final String name;
private final List<Playlist> playlists = new
ArrayList<Playlist>();
/**
* Add a playlist to the composer.
*
* @param playlist the playlist to add
*/
public Playlist addPlaylist(Playlist playlist) {
249
GWT
playlists.add(playlist);
return playlist;
}
/**
* Return the rockin' playlist for this composer.
*/
public List<Playlist> getPlaylists() {
return playlists;
}
}
/**
* The model that defines the nodes in the tree.
*/
private static class CustomTreeModel implements TreeViewModel {
/**
* This selection model is shared across all leaf nodes.
* A selection model can also be shared across all nodes
* in the tree, or each set of child nodes can have its
* own instance. This gives you flexibility to determine
* how nodes are selected.
*/
private final SingleSelectionModel<String> selectionModel
= new SingleSelectionModel<String>();
public CustomTreeModel() {
250
GWT
// Create a database of information.
composers = new ArrayList<Composer>();
GWT
symphonies.addSong("No. 6 - F Major");
symphonies.addSong("No. 7 - A Major");
symphonies.addSong("No. 8 - F Major");
symphonies.addSong("No. 9 - D Minor");
}
GWT
symphonies.addSong("No. 4 - E Minor");
}
/**
* Get the {@link NodeInfo} that provides the children of the
specified
* value.
*/
public <T> NodeInfo<?> getNodeInfo(T value) {
if (value == null) {
// LEVEL 0.
// We passed null as the root value. Return the composers.
GWT
public void render(Composer value, Object key,
SafeHtmlBuilder sb) {
sb.appendEscaped(value.getName());
}
};
// Return a node info that pairs the data provider and the
cell.
return new DefaultNodeInfo<Composer>(dataProvider, cell);
} else if (value instanceof Composer) {
// LEVEL 1.
// We want the children of the composer. Return the playlists.
ListDataProvider<Playlist> dataProvider
= new ListDataProvider<Playlist>(
((Composer) value).getPlaylists());
Cell<Playlist> cell = new AbstractCell<Playlist>() {
@Override
public void render(Playlist value, Object key,
SafeHtmlBuilder sb) {
if (value != null) {
sb.appendEscaped(value.getName());
}
}
};
return new DefaultNodeInfo<Playlist>(dataProvider, cell);
} else if (value instanceof Playlist) {
// LEVEL 2 - LEAF.
// We want the children of the playlist. Return the songs.
ListDataProvider<String> dataProvider
= new ListDataProvider<String>(
((Playlist) value).getSongs());
GWT
selectionModel, null);
}
return null;
}
/**
* Check if the specified value represents a leaf node.
* Leaf nodes cannot be opened.
*/
public boolean isLeaf(Object value) {
// The leaf nodes are the songs, which are Strings.
if (value instanceof String) {
return true;
}
return false;
}
}
/*
* Create the browser using the model. We use <code>null</code> as
the
* default value of the root node. The default value will be
passed to
* CustomTreeModel#getNodeInfo();
*/
CellBrowser browser = new CellBrowser(model, null);
browser.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
browser.setHeight("200");
browser.setWidth("630");
255
GWT
VerticalPanel panel = new VerticalPanel();
panel.setBorderWidth(1);
panel.add(browser);
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:
256
10.
GWT
Layout Panels can contain other widgets. These panels controls the way widgets
to be shown on User Interface. Every Panel widget inherits properties from
Panel class which in turn inherits properties from Widget class and which in turn
inherits properties from UIObject class.
S.N.
1
using
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.UIObject
class:
257
GWT
public abstract class UIObject
extends java.lang.Object
Field
Following are the fields for com.google.gwt.user.client.ui.UIObject class:
public static final java.lang.String DEBUG_ID_PREFIX -- The element ID
that you specify will be prefixed by the static string DEBUG_ID_PREFIX.
Class Constructors
S.N.
1
Class Methods
S.N.
1
ensureDebugId(java.lang.String id)
Ensure that the main Element for this UIObject has an ID property set,
which allows it to integrate with third-party libraries and test tools.
258
GWT
6
int getAbsoluteLeft()
Gets the object's absolute left position in pixels, as measured from the
browser window's client area.
int getAbsoluteTop()
Gets the object's absolute top position in pixels, as measured from the
browser window's client area.
Element getElement()
Gets a handle to the object's underlying DOM element.
int getOffsetHeight()
Gets the object's offset height in pixels.
10
int getOffsetWidth()
Gets the object's offset width in pixels.
11
12
java.lang.String getStyleName()
Gets all of the object's style names, as a space-separated list.
13
14
java.lang.String getStylePrimaryName()
Gets the primary style name associated with the object.
15
16
java.lang.String getTitle()
Gets the title associated with this object.
259
GWT
17
boolean isVisible()
Determines whether or not this object is visible.
18
19
20
21
22
23
24
25
26
27
260
GWT
28
29
30
protected
static
void
java.lang.String style)
setStylePrimaryName(Element
elem,
Sets the element's primary style name and updates all dependent style
names.
31
32
33
34
35
36
java.lang.String toString()
This method is overridden so that any object can be viewed in the
debugger as an HTML snippet.
37
261
GWT
Methods Inherited
This class inherits methods from the following classes:
java.lang.Object
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 Constructors
S.N.
Widget()
This creates a Widget for the child classes.
Class Methods
S.N.
protected
<H
extends
EventHandler>
HandlerRegistration
262
GWT
addDomHandler(H handler, DomEvent.Type<H> type)
Adds a native event handler to the widget and sinks the corresponding
native event.
2
protected
<H
extends
EventHandler>
addHandler(H handler, GwtEvent.Type<H> type)
HandlerRegistration
Widget getParent()
Gets this widget's parent panel.
boolean isAttached()
Determines whether this widget is currently attached to the browser's
document (i.e., there is an unbroken chain of widgets between this
widget and the underlying browser document).
10
263
GWT
11
12
13
14
15
16
void removeFromParent()
Removes this widget from its parent widget.
17
Methods inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Panel class:
public abstract class Panel
264
GWT
extends Widget
implements HasWidgets.ForIsWidget
Field
Following are the fields for com.google.gwt.user.client.ui.Panel class:
Class Constructors
S.N.
Panel()
This creates a Panel for the child classes.
Class methods
S.N.
void clear()
GWT
Deprecated. Use orphan(Widget).
8
10
11
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
Layout Panels
Following are few important Layout Panels:
S.N.
FlowPanel
This widget represents a panel that formats its child widgets using the
default HTML layout behavior.
HorizontalPanel
266
GWT
This widget represents a panel that lays all of its widgets out in a
single horizontal column.
3
VerticalPanel
This widget represents a panel that lays all of its widgets out in a
single vertical column.
HorizontalSplitPanel
This widget represents a panel that arranges two widgets in a single
horizontal row and allows the user to interactively change the
proportion of the width dedicated to each of the two widgets. Widgets
contained within a HorizontalSplitPanel will be automatically decorated
with scrollbars when necessary.
VerticalSplitPanel
This widget represents a A panel that arranges two widgets in a single
vertical column and allows the user to interactively change the
proportion of the height dedicated to each of the two widgets. Widgets
contained within a VertialSplitPanel will be automatically decorated
with scrollbars when necessary.
FlexTable
This widget represents a flexible table that creates cells on demand. It
can be jagged (that is, each row can contain a different number of
cells) and individual cells can be set to span multiple rows or columns.
Grid
This widget represents a A rectangular grid that can contain text, html,
or a child Widget within its cells. It must be resized explicitly to the
desired number of rows and columns.
DeckPanel
panel that displays all of its child widgets in a 'deck', where only one
can be visible at a time. It is used by TabPanel.
DockPanel
This widget represents a panel that lays its child widgets out "docked"
at its outer edges, and allows its last widget to take up the remaining
space in its center.
267
GWT
10
HTMLPanel
This widget represents a panel that contains HTML, and which can
attach child widgets to identified elements within that HTML.
11
TabPanel
This widget represents a panel that represents a tabbed set of pages,
each of which contains another widget. Its child widgets are shown as
the user selects the various tabs associated with them. The tabs can
contain arbitrary HTML.
12
Composite
This widget represents a type of widget that can wrap another widget,
hiding the wrapped widget's methods. When added to a panel, a
composite behaves exactly as if the widget it wraps had been added.
13
SimplePanel
This widget represents a Base class for panels that contain only one
widget.
14
ScrollPanel
This widget represents a simple panel that wraps its contents in a
scrollable area
15
FocusPanel
This widget represents a simple panel that makes its contents
focusable, and adds the ability to catch mouse and keyboard events.
16
FormPanel
This widget represents a panel that wraps its contents in an HTML
<FORM> element.
17
PopupPanel
This widget represents a panel that can pop up over other widgets. It
overlays the browser's client area (and any previously-created
popups).
18
DialogBox
This widget represents a form of popup that has a caption area at the
268
GWT
top and can be dragged by the user. Unlike a PopupPanel, calls to
PopupPanel.setWidth(String) and PopupPanel.setHeight(String) will set
the width and height of the dialog box itself, even if a widget has not
been added as yet.
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.FlowPanel class:
public class FlowPanel
extends ComplexPanel
implements InsertPanel.ForIsWidget
Class Constructors
S.N.
1
Class Methods
S.N.
1
void clear()
Removes all child widgets.
269
GWT
3
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
270
GWT
<!-- Inherit the default GWT style sheet.
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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 {
margin: 10px;
}
GWT
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
FlowPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
GWT
decoratorPanel.setWidth("500");
decoratorPanel.add(flowPanel);
273
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.HorizontalPanel
class:
public class HorizontalPanel
extends CellPanel
implements HasAlignment, InsertPanel.ForIsWidget
Class Constructors
S.N.
HorizontalPanel()
Constructor for empty horizontal Panel.
Class methods
S.N.
void add(Widget w)
Adds a child widget.
HasHorizontalAlignment.HorizontalAlignmentConstant
getHorizontalAlignment()
Gets the horizontal alignment.
HasVerticalAlignment.VerticalAlignmentConstant
getVerticalAlignment()
Gets the vertical alignment.
GWT
7
boolean remove(Widget w)
Removes a child widget.
void
setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant
align)
Sets the default vertical alignment to be used for widgets added to
this panel.
Methods inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
com.google.gwt.user.client.ui.CellPanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
GWT
Following
is
the
content
of
the
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
modified
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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 {
margin: 10px;
}
Following is the content of the modified HTML host file war/HelloWorld.html.
276
GWT
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
HorizontalPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
GWT
CheckBox checkBox = new CheckBox("Item" + i);
horizontalPanel.add(checkBox);
}
Class Ceclaration
Following is the declaration for com.google.gwt.user.client.ui.VerticalPanel
class:
public class VerticalPanel
278
GWT
extends CellPanel
implements HasAlignment, InsertPanel.ForIsWidget
Class Constructors
S.N.
VerticalPanel()
Constructor for empty vertical Panel.
Class Methods
S.N.
void add(Widget w)
Adds a child widget.
Horizontal
Alignment
Constant
get
HasVerticalAlignment.VerticalAlignmentConstant getVerticalAlignment()
Gets the vertical alignment.
boolean remove(Widget w)
Removes a child widget.
GWT
Alignment Constant align)
Sets the default horizontal alignment to be used for widgets added to
this panel.
9
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
com.google.gwt.user.client.ui.CellPanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
modified
module
GWT
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
GWT
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
VerticalPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.RootPanel;
GWT
for(int i = 1;
283
GWT
Class Declaration
Following
is
the
declaration
com.google.gwt.user.client.ui.HorizontalSplitPanel class:
for
@Deprecated
284
GWT
public final class HorizontalSplitPanel
extends Panel
Class Constructors
S.N.
1
HorizontalSplitPanel(HorizontalSplitPanel.Resources resources)
Deprecated. Creates an empty horizontal split panel.
HorizontalSplitPanel(HorizontalSplitPanel(HorizontalSplitPanelImages
images))
Deprecated. replaced by HorizontalSplitPanel(Resources)
Class Methods
S.N.
1
Widget getEndOfLineWidget()
Deprecated. Gets the widget in the pane that is at the end of the line
285
GWT
direction for the layout.
4
Widget getLeftWidget()
Deprecated. Gets the widget in the left side of the panel.
Widget getRightWidget()
Deprecated. Gets the widget in the right side of the panel.
Widget getStartOfLineWidget()
Deprecated. Gets the widget in the pane that is at the start of the line
direction for the layout.
boolean isResizing()
Deprecated. Indicates whether the split panel is being resized.
10
java.util.Iterator<Widget> iterator()
Deprecated. Gets an iterator for the contained widgets.
11
12
13
286
GWT
14
15
16
void setEndOfLineWidget(Widget w)
Deprecated. Sets the widget in the pane that is at the end of the line
direction for the layout.
17
void setLeftWidget(Widget w)
Deprecated. Sets the widget in the left side of the panel.
18
void setRightWidget(Widget w)
Deprecated. Sets the widget in the right side of the panel.
19
20
void setStartOfLineWidget(Widget w)
Deprecated. Sets the widget in the pane that is at the start of the line
direction for the layout.
21
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
java.lang.Object
287
GWT
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
288
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
HorizontalSplitPanel widget.
package com.tutorialspoint.client;
289
GWT
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.VerticalSplitPanel
class:
@Deprecated
public final class VerticalSplitPanel
291
GWT
extends Panel
Class Constructors
S.N.
1
VerticalSplitPanel(VerticalSplitPanel.Resources resources)
Deprecated. Creates an empty vertical split panel.
VerticalSplitPanel(VerticalSplitPanelImages images)
Deprecated. replaced by VerticalSplitPanel(Resources)
Class Methods
S.N.
1
Widget getEndOfLineWidget()
Deprecated. Gets the widget in the pane that is at the end of the line
direction for the layout.
292
GWT
4
Widget getBottomWidget()
Deprecated. Gets the widget in the bottom side of the panel.
Widget getTopWidget()
Deprecated. Gets the widget in the top side of the panel.
Widget getStartOfLineWidget()
Deprecated. Gets the widget in the pane that is at the start of the line
direction for the layout.
boolean isResizing()
Deprecated. Indicates whether the split panel is being resized.
10
java.util.Iterator<Widget> iterator()
Deprecated. Gets an iterator for the contained widgets.
11
12
13
14
GWT
detached from the browser's document.
15
16
void setEndOfLineWidget(Widget w)
Deprecated. Sets the widget in the pane that is at the end of the line
direction for the layout.
17
void setBottomWidget(Widget w)
Deprecated. Sets the widget in the bottom side of the panel.
18
void setTopWidget(Widget w)
Deprecated. Sets the widget in the top side of the panel.
19
20
void setStartOfLineWidget(Widget w)
Deprecated. Sets the widget in the pane that is at the start of the line
direction for the layout.
21
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
java.lang.Object
GWT
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
295
GWT
}
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
VerticalSplitPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
296
GWT
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalSplitPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
297
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.FlowPanel
class:
public class FlexTable
extends HTMLTable
298
GWT
Class Constructors
S.N.
1
Class Methods
S.N.
1
FlexTable.FlexCellFormatter getFlexCellFormatter()
Explicitly gets the FlexTable.FlexCellFormatter.
int getRowCount()
Gets the number of rows.
void removeAllRows()
299
GWT
Remove all rows in this table.
10
11
12
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.HTMLTable
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
the
result
module
of
the
descriptor
300
GWT
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
}
.flexTable td {
border: 1px solid #BBBBBB;
padding: 3px;
}
301
GWT
.flexTable-buttonPanel td {
border: 0px;
}
.fixedWidthButton {
width: 150px;
}
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
FlexTable widget.
package com.tutorialspoint.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.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
302
GWT
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
0, 1,
HasHorizontalAlignment.ALIGN_LEFT);
flexTable.setHTML(0,
0,
0, 2);
@Override
303
GWT
public void onClick(ClickEvent event) {
addRow(flexTable);
}
});
addRowButton.addStyleName("fixedWidthButton");
removeRowButton.addStyleName("fixedWidthButton");
304
GWT
/**
* Add a row to the flex table.
*/
private void addRow(FlexTable flexTable) {
int numRows = flexTable.getRowCount();
flexTable.setWidget(numRows, 0,
new Image("http://www.tutorialspoint.com/images/gwt-mini.png"));
flexTable.setWidget(numRows, 1,
new Image("http://www.tutorialspoint.com/images/gwt-mini.png"));
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
}
/**
* Remove a row from the flex table.
*/
private void removeRow(FlexTable flexTable) {
int numRows = flexTable.getRowCount();
if (numRows > 1) {
flexTable.removeRow(numRows - 1);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows 1);
}
}
}
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:
305
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Grid class:
public class Grid
extends HTMLTable
Class constructors
S.N.
306
GWT
1
Grid()
Constructor for Grid.
Class Methods
S.N.
1
int getColumnCount()
Gets the number of columns in this grid.
int getRowCount()
Return number of rows.
GWT
Checks that the row index is valid.
10
11
12
13
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.HTMLTable
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
GWT
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
309
GWT
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of Grid
widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
GWT
int numColumns = grid.getColumnCount();
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numColumns; col++) {
grid.setWidget(row, col,
new Image("http://www.tutorialspoint.com/images/gwtmini.png"));
}
}
311
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.DeckPanel class:
public class DeckPanel
extends ComplexPanel
implements HasAnimation, InsertPanel.ForIsWidget
Class Constructors
S.N.
312
GWT
1
DeckPanel()
Constructor for DeckPanel.
Class Methods
S.N.
1
int getVisibleWidget()
Gets the index of the currently-visible widget.
boolean isAnimationEnabled()
Returns true if animations are enabled, false if not.
boolean remove(Widget w)
Removes a child widget.
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
313
GWT
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
314
GWT
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;
}
.deckpanel {
border: 1px solid #BBBBBB;
padding: 3px;
}
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>
</body>
</html>
315
GWT
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
DeckPanel widget.
package com.tutorialspoint.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.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
//show first label
deckPanel.showWidget(0);
//create button bar
HorizontalPanel buttonBar = new HorizontalPanel();
buttonBar.setSpacing(5);
buttonBar.add(button1);
buttonBar.add(button2);
317
GWT
buttonBar.add(button3);
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:
318
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.DockPanel
class:
@Deprecated
public class DockPanel
extends CellPanel
implements HasAlignment
Class Constructors
S.N.
1
Class Methods
S.N.
1
HasHorizontalAlignment.HorizontalAlignmentConstant
getHorizontalAlignment()
Deprecated. Gets the horizontal alignment.
HasVerticalAlignment.VerticalAlignmentConstant getVerticalAlignment()
Deprecated. Gets the vertical alignment.
319
GWT
4
DockPanel.DockLayoutConstant getWidgetDirection(Widget w)
Deprecated. Gets the layout direction of the given child widget.
boolean remove(Widget w)
Deprecated. Removes a child widget.
10
11
12
void
setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant
align)
Deprecated. Sets the default vertical alignment to be used for widgets
added to this panel.
320
GWT
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
com.google.gwt.user.client.ui.CellPanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
the
application
to
verify
the
result
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
of
the
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
321
GWT
-->
<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;
}
.dockpanel td {
border: 1px solid #BBBBBB;
padding: 3px;
}
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>
322
GWT
<h1>DockPanel Widget Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
DockPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
dockPanel.add(new HTML("This is the second north component."),
DockPanel.NORTH);
dockPanel.add(new HTML("This is the second south component"),
DockPanel.SOUTH);
324
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.HTMLPanel class:
public class HTMLPanel
extends ComplexPanel
Class Constructors
S.N.
1
325
GWT
2
HTMLPanel(java.lang.String html)
Creates an HTML panel with the specified HTML contents inside a DIV
element.
Class Methods
S.N.
void
addAndReplaceElement(Widget
toReplace)
widget,
Element
Methods Inherited
This class inherits methods from the following classes:
326
GWT
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.ComplexPanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
327
GWT
<!-- 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>
</body>
</html>
328
GWT
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
HTMLPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.TabPanel class:
@Deprecated
public class TabPanel
extends Composite
implements TabListener, SourcesTabEvents,
330
GWT
HasWidgets, HasAnimation, IndexedPanel.ForIsWidget,
HasBeforeSelectionHandlers<java.lang.Integer>,
HasSelectionHandlers<java.lang.Integer>
Class Constructors
S.N.
1
Class Methods
S.N.
1
void add(Widget w)
Deprecated. Adds a child widget.
331
GWT
7
Handler
(Selection
Handler
11
void clear()
Deprecated. Removes all child widgets.
12
13
DeckPanel getDeckPanel()
Deprecated. Gets the deck panel within this tab panel.
14
TabBar getTabBar()
Deprecated. Gets the tab bar within this tab panel.
15
16
int getWidgetCount()
Deprecated. Gets the number of child widgets in this panel.
332
GWT
17
18
19
20
21
void
insert(IsWidget
beforeIndex)
widget,
java.lang.String
tabText,
int
23
24
25
boolean isAnimationEnabled()
Deprecated. Returns true if animations are enabled, false if not.
26
java.util.Iterator<Widget> iterator()
Deprecated. Gets an iterator for the contained widgets.
333
GWT
27
28
29
30
31
32
33
34
35
334
GWT
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Composite
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as explained in the GWT - Create
Application chapter.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
335
GWT
<!-- 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>
</body>
336
GWT
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
TabPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
//create tabs
tabPanel.add(label1, tab1Title);
337
GWT
tabPanel.add(label2, tab2Title);
tabPanel.add(label3, tab3Title);
338
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.Composite class:
public abstract class Composite
extends Widget
339
GWT
Class Constructors
S.N.
1
Class Methods
S.N.
1
boolean isAttached()
Determines whether this widget is currently attached to the browser's
document (i.e., there is an unbroken chain of widgets between this
widget and the underlying browser document).
340
GWT
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
341
GWT
<!-- 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;
}
</body>
342
GWT
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Composite widget.
package com.tutorialspoint.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.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* A composite of a TextBox and a CheckBox that optionally enables
it.
*/
private static class OptionalTextBox extends Composite implements
ClickHandler {
/**
* Constructs an OptionalTextBox with the given caption
* on the check.
* @param caption the caption to be displayed with the check box
*/
public OptionalTextBox(String caption) {
343
GWT
// Place the check above the text box using a vertical panel.
VerticalPanel panel = new VerticalPanel();
// panel.setBorderWidth(1);
panel.setSpacing(10);
panel.add(checkBox);
panel.add(textBox);
textBox.setWidth("200");
// Set the check box's caption, and check it by default.
checkBox.setText(caption);
checkBox.setValue(true);
checkBox.addClickHandler(this);
decoratorPanel.add(panel);
// All composites must call initWidget() in their
constructors.
initWidget(decoratorPanel);
}
GWT
}
}
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:
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.SimplePanel class:
public class SimplePanel
extends Panel
implements HasOneWidget
345
GWT
Class Constructors
S.N.
SimplePanel()
Creates an empty panel that uses a DIV for its contents.
Class Methods
S.N.
void add(Widget w)
Adds a widget to this panel.
Widget getWidget()
Gets the panel's child widget.
java.util.Iterator<Widget> iterator()
Gets an iterator for the contained widgets.
boolean remove(Widget w)
Removes a child widget.
void setWidget(IsWidget w)
Set the only widget of the receiver, replacing the previous widget if
there was one.
void setWidget(Widget w)
Sets this panel's widget.
346
GWT
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
347
GWT
<!-- 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>
</body>
348
GWT
</html>
Let
us
have
following
content
src/com.tutorialspoint/HelloWorld.java which will
SimplePanel widget.
of
Java
file
demonstrate use of
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.ScrollPanel class:
public class ScrollPanel
extends SimplePanel
implements SourcesScrollEvents, HasScrollHandlers,
350
GWT
RequiresResize, ProvidesResize
Class Constructors
S.N.
ScrollPanel()
Creates an empty scroll panel.
ScrollPanel(Widget child)
Creates a new scroll panel with the given child widget.
Class methods
S.N.
Use
int getHorizontalScrollPosition()
Gets the horizontal scroll position.
int getScrollPosition()
351
GWT
Gets the vertical scroll position.
7
void onResize()
This method must be called whenever the implementor's size has been
modified.
void scrollToBottom()
Scroll to the bottom of this panel.
10
void scrollToLeft()
Scroll to the far left of this panel.
11
void scrollToRight()
Scroll to the far right of this panel.
12
void scrollToTop()
Scroll to the top of this panel.
13
14
15
16
352
GWT
17
18
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.SimplePanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
353
GWT
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
354
GWT
<body>
<h1>ScrollPanel Widget Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
ScrollPanel widget.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
GWT
decoratorPanel.add(scrollPanel);
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.FocusPanel class:
public class FocusPanel
extends SimplePanel
implements HasFocus, SourcesClickEvents,
SourcesMouseEvents, SourcesMouseWheelEvents,
HasAllMouseHandlers, HasClickHandlers,
HasDoubleClickHandlers, HasAllKeyHandlers,
HasAllFocusHandlers
Class Constructors
S.N.
FocusPanel()
Creates an empty focus panel.
FocusPanel(Widget child)
Creates a new focus panel with the given child widget.
Class Methods
S.N.
GWT
Adds a DoubleClickEvent handler.
5
10
11
HandlerRegistration addMouseDownHandler(MouseDownHandler
handler)
Adds a MouseDownEvent handler.
12
358
GWT
13
14
15
16
17
HandlerRegistration addMouseWheelHandler(MouseWheelHandler
handler)
Adds a MouseWheelEvent handler.
18
19
int getTabIndex()
Gets the widget's position in the tab index.
20
21
22
GWT
the object returned by an add*Handler method instead
23
24
25
26
27
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.SimplePanel
java.lang.Object
Description
360
GWT
1
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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{
361
GWT
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>
</body>
</html>
Let
us
have
following
content
src/com.tutorialspoint/HelloWorld.java which will
FocusPanel widget.
of
Java
file
demonstrate use of
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
362
GWT
decoratorPanel.add(focusPanel);
363
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.FormPanel class:
public class FormPanel
extends SimplePanel
implements FiresFormEvents,
com.google.gwt.user.client.ui.impl.FormPanelImplHost
Class Constructors
S.N.
GWT
1
FormPanel()
Creates a new FormPanel.
FormPanel(NamedFrame frameTarget)
Creates a FormPanel that targets a NamedFrame.
FormPanel(java.lang.String target)
Creates a new FormPanel.
Class Methods
S.N.
1
Handler
Registration
addSubmit
Complete
(FormPanel.SubmitCompleteHandler handler)
Handler
HandlerRegistration
addSubmitHandler(FormPanel.SubmitHandler handler)
Adds a FormPanel.SubmitEvent handler.
java.lang.String getAction()
365
GWT
Gets the 'action' associated with this form.
5
java.lang.String getEncoding()
Gets the encoding used for submitting this form.
java.lang.String getMethod()
Gets the HTTP method used for submitting this form.
java.lang.String getTarget()
Gets the form's 'target'.
10
boolean onFormSubmit()
Fired when a form is submitted.
11
void onFrameLoad()
12
13
void reset()
Resets the form, clearing all fields.
14
15
GWT
16
17
void submit()
Submits the form.
18
19
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.SimplePanel
java.lang.Object
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
367
GWT
logic.
Following
is
the
content
of
the
src/com.tutorialspoint/HelloWorld.gwt.xml.
modified
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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;
}
368
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
FormPanel widget.
package com.tutorialspoint.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.DecoratorPanel;
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.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
369
GWT
import com.google.gwt.user.client.ui.VerticalPanel;
tb.setName("textBoxFormElement");
panel.add(tb);
GWT
lb.addItem("item3", "item3");
lb.setWidth("220");
panel.add(lb);
form.addSubmitCompleteHandler(new
FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
371
GWT
// 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
// here.
Window.alert(event.getResults());
}
});
}
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:
372
GWT
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.PopupPanel class:
public class PopupPanel
extends SimplePanel
implements SourcesPopupEvents, EventPreview,
HasAnimation, HasCloseHandlers<PopupPanel>
373
GWT
Class Constructors
S.N.
1
PopupPanel(boolean autoHide)
Creates an empty popup panel, specifying its auto-hide property.
popup
panel,
specifying
its auto-
Class Methods
S.N.
1
HandlerRegistration addCloseHandler(CloseHandler<PopupPanel>
handler)
Adds a CloseEvent handler.
void center()
Centers the popup in the browser window and shows it.
GWT
6
java.lang.String getGlassStyleName()
Gets the style name to be used on the glass element.
int getOffsetHeight()
Gets the panel's offset height in pixels.
int getOffsetWidth()
Gets the panel's offset width in pixels.
10
int getPopupLeft()
Gets the popup's left position relative to the browser's client area.
11
int getPopupTop()
Gets the popup's top position relative to the browser's client area.
12
13
java.lang.String getTitle()
Gets the title associated with this object.
14
void hide()
Hides the popup and detaches it from the page.
15
16
boolean isAnimationEnabled()
Returns true if animations are enabled, false if not.
17
boolean isAutoHideEnabled()
375
GWT
Returns true if the popup should be automatically hidden when the
user clicks outside of it.
18
boolean isAutoHideOnHistoryEventsEnabled()
Returns true if the popup should be automatically hidden when the
history token changes, such as when the user presses the browser's
back button.
19
boolean isGlassEnabled()
Returns true if a glass element will be displayed under the PopupPanel.
20
boolean isModal()
Returns true if keyboard or mouse events that do not target the
PopupPanel or its children should be ignored.
21
boolean isPreviewingAllNativeEvents()
Returns true if the popup should preview all native events, even if the
event has already been consumed by another popup.
22
boolean isShowing()
Determines whether or not this popup is showing.
23
boolean isVisible()
Determines whether or not this popup is visible.
24
25
26
GWT
Event) instead
27
28
29
30
31
32
33
34
35
36
GWT
37
38
39
40
41
42
43
44
void setWidget(Widget w)
Sets this panel's widget.
45
46
void show()
Shows the popup and attach it to the page.
47
GWT
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.SimplePanel
java.lang.Object
Step
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
descriptorsrc/com.tutorialspoint/HelloWorld.gwt.xml.
module
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
379
GWT
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
.gwt-PopupPanelGlass {
background-color: #000;
opacity: 0.3;
filter: alpha(opacity=30);
}
380
GWT
.gwt-PopupPanel .popupContent {
border: none;
padding: 3px;
background: gray;
}
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
PopupPanel widget.
package com.tutorialspoint.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;
381
GWT
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
public MyPopup() {
// PopupPanel's constructor takes 'auto-hide' as its boolean
// parameter. If this is set, the panel closes itself
// automatically when the user clicks outside of it.
super(true);
GWT
+" across the screen");
b2.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Create the new popup.
final MyPopup popup = new MyPopup();
// Position the popup 1/3rd of the way down and across
// the screen, and show the popup. Since the position
// calculation is based on the offsetWidth and offsetHeight
// of the popup, you have to use the
// setPopupPositionAndShow(callback)
method. The
alternative
// would be to call show(), calculate
popup.setPopupPosition(left, top);
}
});
}
});
VerticalPanel panel = new VerticalPanel();
panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
panel.setSpacing(10);
panel.add(b1);
panel.add(b2);
GWT
// Add the widgets to the root panel.
RootPanel.get().add(decoratorPanel);
}
}
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:
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.DialogBox class:
384
GWT
public class DialogBox
extends DecoratedPopupPanel
implements HasHTML, HasSafeHtml, MouseListener
Class Constructors
S.N.
DialogBox()
Creates an empty dialog box.
DialogBox(boolean autoHide)
Creates an empty dialog box, specifying its auto-hide property.
dialog
box,
specifying
its auto-
Class Methods
S.N.
1
385
GWT
4
DialogBox.Caption getCaption()
Provides access to the dialog's caption.
java.lang.String getHTML()
Gets this object's contents as HTML.
java.lang.String getText()
Gets this object's text.
void hide()
Hides the popup and detaches it from the page.
10
11
12
13
GWT
14
15
16
17
protected
event)
18
void
onPreviewNativeEvent(Event.NativePreviewEvent
20
21
void show()
Shows the popup and attach it to the page.
Methods Inherited
This class inherits methods from the following classes:
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.Panel
com.google.gwt.user.client.ui.SimplePanel
com.google.gwt.user.client.ui.PopupPanel
com.google.gwt.user.client.ui.DecoratedPopupPanel
java.lang.Object
387
GWT
Description
Create
a
project
with
a
name HelloWorld under
a
package com.tutorialspoint as
explained
in
the GWT
- Create
Application chapter.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
388
GWT
<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-DialogBox .Caption {
background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px;
padding: 4px 4px 4px 8px;
cursor: default;
border-bottom: 1px solid #bbbbbb;
border-top: 5px solid #d0e4f6;
}
.gwt-DialogBox .dialogContent {
}
.gwt-DialogBox .dialogMiddleCenter {
padding: 3px;
background: white;
}
.gwt-DialogBox .dialogBottomCenter {
389
GWT
background: url(images/hborder.png) repeat-x 0px -4px;
-background: url(images/hborder_ie6.png) repeat-x 0px -4px;
}
.gwt-DialogBox .dialogMiddleLeft {
background: url(images/vborder.png) repeat-y;
}
.gwt-DialogBox .dialogMiddleRight {
background: url(images/vborder.png) repeat-y -4px 0px;
-background: url(images/vborder_ie6.png) repeat-y -4px 0px;
}
.gwt-DialogBox .dialogTopLeftInner {
width: 5px;
zoom: 1;
}
.gwt-DialogBox .dialogTopRightInner {
width: 8px;
zoom: 1;
}
.gwt-DialogBox .dialogBottomLeftInner {
width: 5px;
height: 8px;
zoom: 1;
}
.gwt-DialogBox .dialogBottomRightInner {
width: 5px;
height: 8px;
zoom: 1;
}
390
GWT
.gwt-DialogBox .dialogTopLeft {
background: url(images/corner.png) no-repeat -13px 0px;
-background: url(images/corner_ie6.png) no-repeat -13px 0px;
}
.gwt-DialogBox .dialogTopRight {
background: url(images/corner.png) no-repeat -18px 0px;
-background: url(images/corner_ie6.png) no-repeat -18px 0px;
}
.gwt-DialogBox .dialogBottomLeft {
background: url(images/corner.png) no-repeat 0px -15px;
-background: url(images/corner_ie6.png) no-repeat 0px -15px;
}
.gwt-DialogBox .dialogBottomRight {
background: url(images/corner.png) no-repeat -5px -15px;
-background: url(images/corner_ie6.png) no-repeat -5px -15px;
}
html>body .gwt-DialogBox {
}
391
GWT
* html .gwt-DialogBox .dialogBottomLeftInner {
width: 5px;
height: 8px;
overflow: hidden;
}
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
DialogBox widget.
package com.tutorialspoint.client;
392
GWT
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.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
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 MyDialog() {
// Set the dialog box's caption.
setText("My First Dialog");
// Enable animation.
setAnimationEnabled(true);
GWT
setWidget(panel);
}
}
RootPanel.get().add(b);
}
}
394
GWT
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:
395
11.
GWT
GWT provides a event handler model similar to Java AWT or SWING User
Interface frameworks.
A listener interface defines one or more methods that the widget calls to
announce an event. GWT provides a list of interfaces corresponding to
various possible events.
For example, the Button class publishes click events so that you will have to
write a class to implement ClickHandler to handle a click event.
GWT
Button button = new Button("Click Me!");
button.addClickHandler(new MyClickHandler());
Each widget supporting an event type will have a method of the form
HandlerRegistration addFooHandler(FooEvent) where Foo is the actual event
like Click, Error, KeyPress etc.
Following is the list of important GWT event handlers and associated events and
handler registration methods:
S.N.
Event Interface
Before Selection
Handler<I>
void
on
Before
Selection
Selection Event<I> event);
Called
fired.
BlurHandler
when
(Before
BeforeSelectionEvent
is
ChangeHandler
ClickHandler
CloseHandler<T>
void on Context
Event event);
Menu(Context Menu
397
GWT
7
Error Handler
Focus Handler
10
Form Panel.Submit
Complete Handler
void
on
Submit
Complete(Form
Panel.Submit Complete Event event) ;
Fired when a form has been submitted
successfully.
11
FormPanel.SubmitHandler
12
13
KeyPressHandler
14
KeyUpHandler
15
LoadHandler
398
GWT
16
MouseDownHandler
void
onMouseDown(MouseDownEvent
event) ;
Called when MouseDown is fired.
17
MouseMoveHandler
void
event);
onMouseMove(MouseMoveEvent
18
MouseOutHandler
19
MouseOverHandler
void
event);
onMouseOver(MouseOverEvent
20
MouseUpHandler
21
MouseWheelHandler
void
onMouseWheel(MouseWheelEvent
event) ;
Called when MouseWheelEvent is fired.
22
ResizeHandler
23
ScrollHandler
24
SelectionHandler<I>
void
event) ;
onSelection(SelectionEvent<I>
void
399
GWT
25
ValueChangeHandler<I>
onValueChange(ValueChangeEvent<I>
event) ;
Called when ValueChangeEvent is fired.
26
Window.ClosingHandler
void
onWindowClosing(Window.ClosingEvent
event) ;
Fired just before the browser window
closes or navigates to a different site.
27
Window.ScrollHandler
void onWindowScroll(Window.ScrollEvent
event) ;
Fired when
scrolled.
the
browser
window
is
Event Methods
As mentioned earlier, each handler has a single method with a single argument
which holds the event object, for example void onClick(ClickEvent event) or
void onKeyDown(KeyDownEvent event). The event objects like ClickEvent and
KeyDownEvent has few common methods which are listed below:
S.N.
protected final boolean isLive() This method returns whether the event
is live.
This
method
400
GWT
Example
This example will take you through simple steps to show usage of a Click Event
and KeyDown Event handling in GWT. Follow the following steps to update the
GWT application, we created in GWT - Create Application chapter:
Step
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
src/com.tutorialspoint/HelloWorld.gwt.xml.
modified
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<source path='client'/>
<source path='shared'/>
</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center;
401
GWT
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>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
Event Handling in GWT.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
402
GWT
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
/*
* create button and attach click handler
*/
Button button = new Button("Click Me!");
button.addClickHandler(new MyClickHandler());
GWT
decoratorPanel.add(panel);
RootPanel.get("gwtContainer").add(decoratorPanel);
}
/**
* create a custom click handler which will call
* onClick method when button is clicked.
*/
private class MyClickHandler implements ClickHandler {
@Override
public void onClick(ClickEvent event) {
Window.alert("Hello World!");
}
}
/**
* create a custom key down handler which will call
* onKeyDown method when a key is down in textbox.
*/
private class MyKeyDownHandler implements KeyDownHandler {
@Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
Window.alert(((TextBox)event.getSource()).getValue());
}
}
}
}
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:
404
GWT
405
12.
GWT
GWT provides three ways to create custom user interface elements. There are
three general strategies to follow:
Create a widget using GWT DOM API in JAVA: GWT basic widgets
are created in this way. Still its a very complicated way to create custom
widget and should be used cautiously.
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
GWT
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<h1>Custom Widget Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate creation of
a Custom widget.
package com.tutorialspoint.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.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
/**
* A composite of a TextBox and a CheckBox that optionally enables
it.
*/
private static class OptionalTextBox extends Composite implements
ClickHandler {
408
GWT
private CheckBox checkBox = new CheckBox();
private boolean enabled = true;
/**
* Style this widget using .optionalTextWidget CSS class.<br/>
* Style textbox using .optionalTextBox CSS class.<br/>
* Style checkbox using .optionalCheckBox CSS class.<br/>
* Constructs an OptionalTextBox with the given caption
* on the check.
* @param caption the caption to be displayed with the check box
*/
public OptionalTextBox(String caption) {
// place the check above the text box using a vertical panel.
HorizontalPanel panel = new HorizontalPanel();
// panel.setBorderWidth(1);
panel.setSpacing(10);
panel.add(checkBox);
panel.add(textBox);
409
GWT
//set style name for text box
textBox.setStyleName("optionalTextBox");
GWT
RootPanel.get().add(otb);
}
}
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:
We've created a widget with GWT inbuilt widgets, TextBox and CheckBox
thus using the concept of reusability.
411
GWT
412
13.
GWT UIBINDER
GWT
Introduction
The UiBinder is a framework designed to separate Functionality and View of
User Interface.
UiBinder Workflow
Step 1: Create UI Declaration XML File
Create a XML/HTML based User Interface declaration file. We've created a
Login.ui.xml file in our example.
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:gwt='urn:import:com.google.gwt.user.client.ui'
xmlns:res='urn:with:com.tutorialspoint.client.LoginResources'>
<ui:with type="com.tutorialspoint.client.LoginResources"
field="res">
</ui:with>
<gwt:HTMLPanel>
...
</gwt:HTMLPanel>
</ui:UiBinder>
GWT
<gwt:Label ui:field="completionLabel2" />
@UiField
Label completionLabel2;
...
}
/*
414
GWT
* @UiTemplate is not mandatory but allows multiple XML templates
* to be used for the same widget.
* Default file loaded will be <class-name>.ui.xml
*/
@UiTemplate("Login.ui.xml")
interface LoginUiBinder extends UiBinder<Widget, Login> {
}
...
}
Java
based
Resource
.blackText {
font-family: Arial, Sans-serif;
color: #000000;
font-size: 11px;
text-align: left;
}
...
...
}
@Source("Login.css")
MyCss style();
415
GWT
}
Description
Modify
HelloWorld.gwt.xml,
HelloWorld.css,
HelloWorld.html
and
HelloWorld.java as explained below. Keep rest of the files unchanged.
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- Inherit the UiBinder module.
-->
<inherits name="com.google.gwt.uibinder.UiBinder"/>
416
GWT
<!-- Specify the app entry point class.
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>UiBinder Demonstration</h1>
<div id="gwtContainer"></div>
417
GWT
</body>
</html>
Now create a new UiBinder template and owner class (File -> New ->
UiBinder).
Choose the client package for the project and then name it Login. Leave all of
the other defaults.
Click Finish button and the plugin will create a new UiBinder template and
owner class.
418
GWT
.redText {
font-family: Arial, Sans-serif;
color: #ff0000;
font-size: 11px;
text-align: left;
419
GWT
}
.loginButton {
border: 1px solid #3399DD;
color: #FFFFFF;
background: #555555;
font-size: 11px;
font-weight: bold;
margin: 0 5px 0 0;
padding: 4px 10px 5px;
text-shadow: 0 -1px 0 #3399DD;
}
.box {
border: 1px solid #AACCEE;
display: block;
font-size: 12px;
margin: 0 0 5px;
padding: 3px;
width: 203px;
}
.background {
background-color: #999999;
border: 1px none transparent;
color: #000000;
font-size: 11px;
margin-left: -8px;
margin-top: 5px;
padding: 6px;
}
Now create LoginResources.java file in the src/com.tutorialspoint/client
package and place the following contents in it
package com.tutorialspoint.client;
420
GWT
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
String redText();
String loginButton();
String box();
String background();
}
@Source("Login.css")
MyCss style();
}
Replace the contents of
package with the following:
Login.ui.xml
in
src/com.tutorialspoint/client
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:gwt='urn:import:com.google.gwt.user.client.ui'
xmlns:res='urn:with:com.tutorialspoint.client.LoginResources'>
<ui:with type="com.tutorialspoint.client.LoginResources"
field="res">
</ui:with>
<gwt:HTMLPanel>
<div align="center">
<gwt:VerticalPanel res:styleName="style.background">
421
GWT
<gwt:Label text="Login" res:styleName="style.blackText" />
<gwt:TextBox ui:field="loginBox" res:styleName="style.box" />
<gwt:Label text="Password" res:styleName="style.blackText" />
<gwt:PasswordTextBox ui:field="passwordBox"
res:styleName="style.box" />
<gwt:HorizontalPanel verticalAlignment="middle">
<gwt:Button ui:field="buttonSubmit" text="Submit"
res:styleName="style.loginButton" />
<gwt:CheckBox ui:field="myCheckBox" />
<gwt:Label ui:field="myLabel" text="Remember me"
res:styleName="style.blackText" />
</gwt:HorizontalPanel>
<gwt:Label ui:field="completionLabel1"
res:styleName="style.blackText" />
<gwt:Label ui:field="completionLabel2"
res:styleName="style.blackText" />
</gwt:VerticalPanel>
</div>
</gwt:HTMLPanel>
</ui:UiBinder>
Replace the contents of Login.java in src/com.tutorialspoint/client package
with the following:
package com.tutorialspoint.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
422
GWT
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
/*
* @UiTemplate is not mandatory but allows multiple XML templates
* to be used for the same widget.
* Default file loaded will be <class-name>.ui.xml
*/
@UiTemplate("Login.ui.xml")
interface LoginUiBinder extends UiBinder<Widget, Login> {
}
@UiField(provided = true)
final LoginResources res;
public Login() {
this.res = GWT.create(LoginResources.class);
res.style().ensureInjected();
initWidget(uiBinder.createAndBindUi(this));
}
@UiField
TextBox loginBox;
@UiField
TextBox passwordBox;
@UiField
Label completionLabel1;
423
GWT
@UiField
Label completionLabel2;
/*
* Method name is not relevant, the binding is done according to the
class
* of the parameter.
*/
@UiHandler("buttonSubmit")
void doClickSubmit(ClickEvent event) {
if (tooShort) {
Window.alert("Login Successful!");
} else {
Window.alert("Login or Password is too short!");
}
}
@UiHandler("loginBox")
void handleLoginChange(ValueChangeEvent<String> event) {
if (event.getValue().length() < 6) {
completionLabel1.setText("Login too short (Size must be > 6)");
tooShort = true;
} else {
tooShort = false;
completionLabel1.setText("");
}
}
@UiHandler("passwordBox")
void handlePasswordChange(ValueChangeEvent<String> event) {
if (event.getValue().length() < 6) {
424
GWT
tooShort = true;
completionLabel2.setText("Password too short (Size must be > 6)");
} else {
tooShort = false;
completionLabel2.setText("");
}
}
}
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java which will demonstrate use of
UiBinder.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
425
GWT
426
14.
GWT
A GWT based application generally consists of a client side module and server
side module. Client side code runs in browser and server side code runs in web
server. Client side code has to make an HTTP request accross the network to
access server side data.
RPC, Remote procedure call is the mechansim used by GWT in which client code
can directly execute the server side methods.
GWT client and server both serialize and deserialize data automatically so
developers are not required to serialize/deserialize objects and data objects can
travel over HTTP.
Following diagram is showing the RPC Architecture.
427
GWT
428
GWT
descriptor
(web.xml)
to
include
429
GWT
<web-app>
...
<servlet>
<servlet-name>messageServiceImpl</servlet-name>
<servlet-class>com.tutorialspoint.server.MessageServiceImpl
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>messageServiceImpl</servlet-name>
<url-pattern>/helloworld/message</url-pattern>
</servlet-mapping>
</web-app>
@Override
public void onFailure(Throwable caught) {
Window.alert("Unable to obtain server response: "
+ caught.getMessage());
}
@Override
public void onSuccess(Message result) {
Window.alert(result.getMessage());
}
}
Call Remote service when user interacts with UI
public class HelloWorld implements EntryPoint {
430
GWT
...
public void onModuleLoad() {
...
buttonMessage.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
messageService.getMessage(txtName.getValue(),
new MessageCallBack());
}});
...
}
}
Description
verify the
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml
result of the
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
431
GWT
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- Inherit the UiBinder module.
-->
<inherits name="com.google.gwt.uibinder.UiBinder"/>
<!-- Specify the app entry point class.
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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>
432
GWT
</body>
</html>
Now create Message.java file in the src/com.tutorialspoint/client package
and place the following contents in it.
package com.tutorialspoint.client;
import java.io.Serializable;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
433
GWT
@RemoteServiceRelativePath("message")
public interface MessageService extends RemoteService {
Message getMessage(String input);
}
Now
create
MessageServiceAsync.java
file
in
the
src/com.tutorialspoint/client package and place the following contents in it
package com.tutorialspoint.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tutorialspoint.client.Message;
import com.tutorialspoint.client.MessageService;
GWT
Update the content of the modified web application deployment descriptor
war/WEB-INF/web.xml to include MessageServiceImpl Servlet declaration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>HelloWorld.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>messageServiceImpl</servlet-name>
<servlet-class>com.tutorialspoint.server.MessageServiceImpl
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>messageServiceImpl</servlet-name>
<url-pattern>/helloworld/message</url-pattern>
</servlet-mapping>
</web-app>
Replace the contents of HelloWorld.java in src/com.tutorialspoint/client
package with the following
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
435
GWT
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
GWT
txtName.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
/* make remote call to server to get the message */
messageService.getMessage(txtName.getValue(),
new MessageCallBack());
}
}
});
Label lblName = new Label("Enter your name: ");
buttonMessage.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
/* make remote call to server to get the message */
messageService.getMessage(txtName.getValue(),
new MessageCallBack());
}});
437
GWT
DecoratorPanel panel = new DecoratorPanel();
panel.add(vPanel);
438
15.
GWT
GWT provides execellent support for automated testing of client side code using
JUnit testing framework. In this article we'll demonstrate GWT and JUNIT
integration.
Archive name
Windows junit4.10.jar
Linux
junit4.10.jar
Mac
junit4.10.jar
Store the downloaded jar file to some location on your computer. We've stored
it at C:/ > JUNIT
Windows C:\GWT\gwt-2.1.0
Linux
/usr/local/GWT/gwt-2.1.0
Mac
/Library/GWT/gwt-2.1.0
GWTTestCase Class
GWT provides GWTTestCase base class which provides JUnit integration.
Running a compiled class which extends GWTTestCase under JUnit launches the
HtmlUnit browser which serves to emulate your application behavior during test
execution.
GWTTestCase is a derived class from JUnit's TestCase and it can be run using
JUnit TestRunner.
439
GWT
Using webAppCreator
GWT provides a special command line tool webAppCreator which can
generate a starter test case for us, plus ant targets and eclipse launch configs
for testing in both development mode and production mode.
Open command prompt and go to C:\ > GWT_WORKSPACE > where you
want to create a new project with test support.Run the following command
C:\GWT_WORKSPACE>C:\GWT\gwt-2.1.0\webAppCreator
-out HelloWorld
-junit C:\JUNIT\junit-4.10.jar
com.tutorialspoint.HelloWorld
Noteworthy Points
GWT
HelloWorld\src\com\tutorialspoint\client\GreetingServiceAsync.java
Created file
HelloWorld\src\com\tutorialspoint\server\GreetingServiceImpl.java
Created file
HelloWorld\src\com\tutorialspoint\shared\FieldVerifier.java
Created file HelloWorld\build.xml
Created file HelloWorld\README.txt
Created file HelloWorld\test\com\tutorialspoint\HelloWorldJUnit.gwt.xml
Created file
HelloWorld\test\com\tutorialspoint\client\HelloWorldTest.java
Created file HelloWorld\.project
Created file HelloWorld\.classpath
Created file HelloWorld\HelloWorld.launch
Created file HelloWorld\HelloWorldTest-dev.launch
Created file HelloWorld\HelloWorldTest-prod.launch
import com.tutorialspoint.shared.FieldVerifier;
import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
/**
* GWT JUnit tests must extend GWTTestCase.
*/
public class HelloWorldTest extends GWTTestCase {
/**
* must refer to a valid module that sources this class.
*/
public String getModuleName() {
441
GWT
return "com.tutorialspoint.HelloWorldJUnit";
}
/**
* tests the FieldVerifier.
*/
public void testFieldVerifier() {
assertFalse(FieldVerifier.isValidName(null));
assertFalse(FieldVerifier.isValidName(""));
assertFalse(FieldVerifier.isValidName("a"));
assertFalse(FieldVerifier.isValidName("ab"));
assertFalse(FieldVerifier.isValidName("abc"));
assertTrue(FieldVerifier.isValidName("abcd"));
}
/**
* this test will send a request to the server using the greetServer
*
*/
public void testGreetingService() {
/* create the service that we will test. */
GreetingServiceAsync greetingService =
GWT.create(GreetingService.class);
ServiceDefTarget target = (ServiceDefTarget) greetingService;
target.setServiceEntryPoint(GWT.getModuleBaseURL()
+ "helloworld/greet");
GWT
greetingService.greetServer("GWT User",
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
/* The request resulted in an unexpected error. */
fail("Request failure: " + caught.getMessage());
}
Noteworthy Points
S.No
Note
GWTTestCase
class
in
the
443
GWT
5
These methods use one of the many assert* functions that it inherits
from the JUnit Assert class, which is an ancestor of GWTTestCase.
Description
Compile and run the application to verify the result of the implemented
logic.
444
GWT
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- Inherit the UiBinder module.
-->
<inherits name="com.google.gwt.uibinder.UiBinder"/>
<!-- Specify the app entry point class.
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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.
445
GWT
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
446
GWT
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
buttonMessage.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert(getGreeting(txtName.getValue()));
}});
GWT
vPanel.setSpacing(10);
vPanel.add(hPanel);
vPanel.add(buttonMessage);
vPanel.setCellHorizontalAlignment(buttonMessage,
HasHorizontalAlignment.ALIGN_RIGHT);
in
package com.tutorialspoint.client;
import com.tutorialspoint.shared.FieldVerifier;
import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
/**
* GWT JUnit tests must extend GWTTestCase.
*/
public class HelloWorldTest extends GWTTestCase {
/**
448
GWT
* must refer to a valid module that sources this class.
*/
public String getModuleName() {
return "com.tutorialspoint.HelloWorldJUnit";
}
/**
* tests the FieldVerifier.
*/
public void testFieldVerifier() {
assertFalse(FieldVerifier.isValidName(null));
assertFalse(FieldVerifier.isValidName(""));
assertFalse(FieldVerifier.isValidName("a"));
assertFalse(FieldVerifier.isValidName("ab"));
assertFalse(FieldVerifier.isValidName("abc"));
assertTrue(FieldVerifier.isValidName("abcd"));
}
/**
* this test will send a request to the server using the greetServer
*
*/
public void testGreetingService() {
/* create the service that we will test. */
GreetingServiceAsync greetingService =
GWT.create(GreetingService.class);
ServiceDefTarget target = (ServiceDefTarget) greetingService;
target.setServiceEntryPoint(GWT.getModuleBaseURL()
+ "helloworld/greet");
GWT
delayTestFinish(10000);
/**
* tests the getGreeting method.
*/
public void testGetGreeting() {
HelloWorld helloWorld = new HelloWorld();
String name = "Robert";
String expectedGreeting = "Hello "+name+"!";
assertEquals(expectedGreeting,helloWorld.getGreeting(name));
}
}
}
450
GWT
From the Eclipse menu bar, select Run > Run Configurations...
Under JUnit section, select HelloWorldTest-dev
To save the changes to the Arguments, press Apply
To run the test, press Run
If everything is fine with your application, this will produce following result:
From the Eclipse menu bar, select Run > Run Configurations...
Under JUnit section, select HelloWorldTest-prod
To save the changes to the Arguments, press Apply
To run the test, press Run
If everything is fine with your application, this will produce following result:
451
GWT
452
16.
GWT
Set break points in the code and see them in BreakPoint Explorer.
Step through the code line by line during debugging.
View the values of variable.
Inspect the values of all the variables.
Inspect the value of an expression.
Display the stack frame for suspended threads.
Debugging Example
This example will take you through simple steps to demonstrate debugging a
GWT application. The following steps will help to you update the GWT
application which we created in GWT - Create Application chapter:
Step
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
453
GWT
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
-->
<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-Label{
font-size: 150%;
font-weight: bold;
color:red;
padding:5px;
454
GWT
margin:5px;
}
Following is the content of the modified HTML host file war/HelloWorld.html
to accommodate two buttons.
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
</body>
</html>
Let
us
have
the
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java using which we will demonstrate
the debugging capability of GWT Code.
package com.tutorialspoint.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.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
455
GWT
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
buttonMessage.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert(getGreeting(txtName.getValue()));
}});
GWT
hPanel.setCellWidth(lblName, "130");
457
GWT
458
GWT
If everything is fine, you must see GWT Development Mode active in Eclipse
containing a URL as shown below. Double click the URL to open the GWT
application.
As soon as the Application launches, you will see the focus on Eclipse
breakpoint as we've placed the breakpoint on the first line of entry point
method.
459
GWT
460
GWT
461
GWT
Now keep pressing F6 until you reach the last line of onModuleLoad() method.
As reference for function keys, F6 inspects the code line by line, F5 steps inside
further, and F8 will resume the application. Now you can see the list of values
of all variables of onModuleLoad() method.
462
GWT
The GWT client code can be debugged in the same way as a Java Application.
Place breakpoints at any line and play with the debugging capabilities of GWT.
463
17.
GWT
GWT INTERNATIONALIZATION
Static String
Internationalization
Description
This technique is most prevalent and requires very little
overhead at runtime; is a very efficient technique for
translating
both
constant
and
parameterized
strings;simplest to implement.
Static string internationalization uses standard Java
properties files to store translated strings and
parameterized messages, and strongly-typed Java
interfaces are created to retrieve their values.
Dynamic String
Internationalization
Localizable
Interface
464
GWT
Create properties files containing the translated values specific to locale. We've
created a HelloWorldMessages_de.properties file in our example. This file
contains translations in german language. _de specifies the german locale and
we're going to support german language in our application.
If you are creating properties file using Eclipse, then change the encoding of
the file to UTF-8. Select the file and then right-click on it to open its properties
window. Select text file encoding as Other UTF-8. Apply and save changes to
the file.
enterName=Geben Sie Ihren Namen
clickMe=Klick mich
applicationTitle=Anwendung Internationalisierung Demonstration
greeting=Hallo {0}
465
GWT
@DefaultMessage("Click Me")
String clickMe();
@DefaultMessage("Hello {0}")
String greeting(String name);
}
GWT
Step
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
<extend-property name="locale" values="de" />
<!-- 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;
467
GWT
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 id="gwtAppTitle"></h1>
<div id="gwtContainer"></div>
</body>
</html>
Now
create
HelloWorldMessages.properties
file
in
the
src/com.tutorialspoint/client package and place the following contents in it
enterName=Enter your name
clickMe=Click Me
applicationTitle=Application Internationalization Demonstration
greeting=Hello {0}
Now
create
HelloWorldMessages_de.properties
file
in
the
src/com.tutorialspoint/client package and place the following contents in it
enterName=Geben Sie Ihren Namen
clickMe=Klick mich
applicationTitle=Anwendung Internationalisierung Demonstration
468
GWT
greeting=Hallo {0}
Now
create
HelloWorldMessages.java
class
in
the
src/com.tutorialspoint/client package and place the following contents in it
package com.tutorialspoint.client;
import com.google.gwt.i18n.client.Messages;
@DefaultMessage("Hello {0}")
String greeting(String name);
}
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java using which we will demonstrate
Internationalization capability of GWT Code.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
469
GWT
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
buttonMessage.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert(getGreeting(txtName.getValue()));
}});
470
GWT
471
GWT
Now
update
the
URL
to
contain
the
locale=de.Set
URL:
http://127.0.0.1:8888/HelloWorld.html?gwt.codesvr=127.0.0.1:9997&locale=
de. If everything is fine with your application, then this will produce the
following result:
472
GWT
473
18.
GWT
GWT applications are normally single page application running JavaScripts and
do not contains lot of pages thus browsers do not keep track of user interaction
with Application. To use browser's history functionality, application should
generate a unique URL fragment for each navigable page.
GWT provides History Mechanism to handle this situation.
GWT uses a term token which is simply a string that the application can parse
to return to a particular state. Application will save this token in browser's
history as URL fragment.
For example, a history token named "pageIndex1" would be added to a URL as
follows:
http://www.tutorialspoint.com/HelloWorld.html#pageIndex0
GWT
@Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
/* parse the history token */
try {
if (historyToken.substring(0, 9).equals("pageIndex")) {
String tabIndexToken = historyToken.substring(9, 10);
int tabIndex = Integer.parseInt(tabIndexToken);
/* select the specified tab panel */
tabPanel.selectTab(tabIndex);
} else {
tabPanel.selectTab(0);
}
} catch (IndexOutOfBoundsException e) {
tabPanel.selectTab(0);
}
}
});
Now let's see the History Class in Action.
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
475
GWT
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.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"/>
476
GWT
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<iframe src="javascript:''"
id="__gwt_historyFrame"
style="width:0;height:0;border:0"></iframe>
<h1> History Class Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java using which we will demonstrate
History Management in GWT Code.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
/**
* This is the entry point method.
*/
477
GWT
public void onModuleLoad() {
/* create a tab panel to carry multiple pages */
final TabPanel tabPanel = new TabPanel();
/* create pages */
HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");
GWT
Back button or Forward button are clicked
and URL of application changes.
*/
History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
/* parse the history token */
try {
if (historyToken.substring(0, 9).equals("pageIndex")) {
String tabIndexToken = historyToken.substring(9, 10);
int tabIndex = Integer.parseInt(tabIndexToken);
/* select the specified tab panel */
tabPanel.selectTab(tabIndex);
} else {
tabPanel.selectTab(0);
}
} catch (IndexOutOfBoundsException e) {
tabPanel.selectTab(0);
}
}
});
479
GWT
You should notice that, when each tab is selected, application url is
changed and #pageIndex is added to the url.
You can also see that browser's back and forward buttons are enabled
now.
Use back and forward button of the browser and you will see the
different tabs get selected accordingly.
480
19.
GWT
GWT supports browser history management using a History class for which you
can reference GWT - History Class chapter.
GWT uses a term token which is simply a string that the application can parse
to return to a particular state. Application will save this token in browser's
history as URL fragment.
In GWT - History Class chapter, we handle the token creation and setting in the
history by writing code.
In this article, we will discuss a special widget Hyperlink which does the token
creation and history management for us automatically and gives application
capability of bookmarking.
Bookmarking Example
This example will take you through some simple steps to demonstrate
bookmarking of a GWT application.
The following steps will help to you update the GWT application which we
created in GWT - Create Application chapter:
Step
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
481
GWT
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
-->
<entry-point class='com.tutorialspoint.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
<title>Hello World</title>
<link rel="stylesheet" href="HelloWorld.css"/>
<script language="javascript"
src="helloworld/helloworld.nocache.js">
</script>
</head>
<body>
<iframe src="javascript:''"
id="__gwt_historyFrame"
style="width:0;height:0;border:0"></iframe>
<h1> Bookmarking Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java using which we will demonstrate
Bookmarking in GWT Code.
package com.tutorialspoint.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
483
GWT
private TabPanel tabPanel;
try {
if (historyToken.substring(0, 9).equals("pageIndex")) {
String tabIndexToken = historyToken.substring(9, 10);
int tabIndex = Integer.parseInt(tabIndexToken);
/* Select the specified tab panel */
tabPanel.selectTab(tabIndex);
} else {
tabPanel.selectTab(0);
}
} catch (IndexOutOfBoundsException e) {
tabPanel.selectTab(0);
}
}
/**
* This is the entry point method.
*/
public void onModuleLoad() {
/* create a tab panel to carry multiple pages */
tabPanel = new TabPanel();
/* create pages */
HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");
484
GWT
Hyperlink firstPageLink = new Hyperlink("1", "pageIndex0");
Hyperlink secondPageLink = new Hyperlink("2", "pageIndex1");
Hyperlink thirdPageLink = new Hyperlink("3", "pageIndex2");
if (initToken.length() == 0) {
History.newItem("pageIndex0");
initToken = "pageIndex0";
}
tabPanel.setWidth("400");
/* add pages to tabPanel*/
tabPanel.add(firstPage, firstPageTitle);
tabPanel.add(secondPage,secondPageTitle);
tabPanel.add(thirdPage, thirdPageTitle);
GWT
}
});
selectTab(initToken);
vPanel.setSpacing(10);
vPanel.add(tabPanel);
vPanel.add(linksHPanel);
486
GWT
Now click on 1, 2 or 3. You can notice that the tab changes with indexes.
You should notice, when you click on 1,2 or 3 ,application url is changed
and #pageIndex is added to the url
You can also see that browser's back and forward buttons are enabled
now.
Use back and forward button of the browser and you will see the
different tabs get selected accordingly.
Right Click on 1, 2 or 3. You can see options like open, open in new
window, open in new tab, add to favourites etc.
Right Click on 3. Choose add to favourites. Save bookmark as page 3.
Open favorites and choose page 3. You will see the third tab selected.
487
20.
GWT
Types of Logger
Loggers are organized in a tree structure, with the Root Logger at the root of
the tree.
Name of the logger determine the Parent/Child relationships using . to separate
sections of the name.
As an example if we have two loggers Hospital.room1 and Hospital.room2, then
they are siblings, with their parent being the logger named Hospital. The
Hospital logger (and any logger with a name which does not contain a dot ".")
has the Root Logger as a parent.
private static Logger room1Logger = Logger.getLogger("Hospital.room1");
private static Logger room2Logger = Logger.getLogger("Hospital.room2");
private static Logger hospitalLogger = Logger.getLogger("Hospital");
private static Logger rootLogger = Logger.getLogger("");
Log Handlers
GWT provides default handlers which will show the log entries made using
loggers.
Handler
Logs to
Description
SystemLogHandler
stdout
DevelopmentModeLogHandler DevMode
Window
ConsoleLogHandler
javascript
488
GWT
console
FirebugLogHandler
Firebug
PopupLogHandler
popup
SimpleRemoteLogHandler
server
GWT
Step
Description
Compile and run the application to verify the result of the implemented
logic.
Following
is
the
content
of
the
modified
src/com.tutorialspoint/HelloWorld.gwt.xml.
module
descriptor
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<inherits name="com.google.gwt.logging.Logging"/>
<!-- Specify the app entry point class.
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
<!-- Specify the paths for translatable code
-->
<source path='client'/>
<source path='shared'/>
<set-property name="gwt.logging.logLevel" value="SEVERE"/>
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.popupHandler" value="DISABLED" />
</module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
body{
text-align: center;
font-family: verdana, sans-serif;
}
h1{
490
GWT
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>
<iframe src="javascript:''"
id="__gwt_historyFrame"
style="width:0;height:0;border:0"></iframe>
<h1> Logging Demonstration</h1>
<div id="gwtContainer"></div>
</body>
</html>
Let
us
have
following
content
of
Java
file
src/com.tutorialspoint/HelloWorld.java using which we will demonstrate
Bookmarking in GWT Code.
package com.tutorialspoint.client;
import java.util.logging.Level;
import java.util.logging.Logger;
491
GWT
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.logging.client.HasWidgetsLogHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
try {
if (historyToken.substring(0, 9).equals("pageIndex")) {
String tabIndexToken = historyToken.substring(9, 10);
int tabIndex = Integer.parseInt(tabIndexToken);
/* Select the specified tab panel */
tabPanel.selectTab(tabIndex);
} else {
tabPanel.selectTab(0);
}
} catch (IndexOutOfBoundsException e) {
tabPanel.selectTab(0);
}
}
492
GWT
/**
* This is the entry point method.
*/
public void onModuleLoad() {
/* create a tab panel to carry multiple pages */
tabPanel = new TabPanel();
/* create pages */
HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");
if (initToken.length() == 0) {
History.newItem("pageIndex0");
initToken = "pageIndex0";
493
GWT
}
tabPanel.setWidth("400");
/* add pages to tabPanel*/
tabPanel.add(firstPage, firstPageTitle);
tabPanel.add(secondPage,secondPageTitle);
tabPanel.add(thirdPage, thirdPageTitle);
selectTab(initToken);
vPanel.setSpacing(10);
vPanel.add(tabPanel);
vPanel.add(linksHPanel);
GWT
rootLogger.addHandler(new HasWidgetsLogHandler(customLogArea));
495
GWT
Now click on 1, 2 or 3. You can notice, when you click on 1,2 or 3 you can see
the log is getting printed displaying the pageIndex. Check the Console output in
Eclipse. You can see the log is getting printed in Eclipse console as well.
Fri Aug 31 11:42:35 IST 2012
SEVERE: pageIndex selected: pageIndex0
Fri Aug 31 11:42:37 IST 2012
SEVERE: pageIndex selected: pageIndex1
496
GWT
Fri Aug 31 11:42:38 IST 2012
SEVERE: pageIndex selected: pageIndex2
Fri Aug 31 11:42:40 IST 2012
SEVERE: pageIndex selected: pageIndex0
Fri Aug 31 11:42:41 IST 2012
SEVERE: pageIndex selected: pageIndex1
Fri Aug 31 11:42:41 IST 2012
SEVERE: pageIndex selected: pageIndex2
Now update module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml
to enable popupHandler.
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='helloworld'>
<!-- Inherit the core Web Toolkit stuff.
-->
<inherits name='com.google.gwt.user.User'/>
-->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<inherits name="com.google.gwt.logging.Logging"/>
<!-- Specify the app entry point class.
-->
<entry-point class='com.tutorialspoint.client.HelloWorld'/>
<!-- Specify the paths for translatable code
-->
<source path='client'/>
<source path='shared'/>
<set-property name="gwt.logging.logLevel" value="SEVERE"/>
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.popupHandler" value="ENABLED" />
</module>
Once you are ready with all the changes done, reload the application by
refreshing the browser window (press F5/reload button of the browser). Notice
a popup window is present now in upper left corner of the application.
Now click on 1, 2 or 3. You can notice, when you click on 1,2 or 3 ,you can see
the log is getting printed displaying the pageIndex in the popup window.
497
GWT
498