When we created the GWT application, a class named
FirstApp
will be created. We created a copy of that class namedFirstGxtApp
.In the imports section of the
FirstGxtApp
class, we removed the following GWT specific imports:import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel;
We then added imports to the equivalent GXT classes as follows:
import com.extjs.gxt.ui.client.event.ButtonEvent; import com.extjs.gxt.ui.client.event.SelectionListener; import com.extjs.gxt.ui.client.event.KeyListener; import com.extjs.gxt.ui.client.event.ComponentEvent; import com.extjs.gxt.ui.client.widget.Dialog; import com.extjs.gxt.ui.client.widget.Label; import com.extjs.gxt.ui.client.widget.VerticalPanel; import com.extjs.gxt.ui.client.widget.button.Button; import com.extjs.gxt.ui.client.widget.form.TextField;
You may notice that some of the GXT classes share a similar name to their GWT equivalents. The following table shows the GXT classes we used and the GWT equivalents:
GXT
GWT
com.extjs.gxt.ui.client.widget.Dialog
com.google.gwt.user.client.ui.DialogBox
com.extjs.gxt.ui.client.widget.Label
com.google.gwt.user.client.ui.Label
com.extjs.gxt.ui.client.widget.VerticalPanel
com.google.gwt.user.client.ui.VerticalPanel
com.extjs.gxt.ui.client.widget.button.Button
com.google.gwt.user.client.ui.Button
com.extjs.gxt.ui.client.widget.form.TextField
com.google.gwt.user.client.ui.TextBox
com.extjs.gxt.ui.client.event.ButtonEvent
com.google.gwt.event.dom.client.ClickEvent
com.extjs.gxt.ui.client.event.SelectionListener
com.google.gwt.event.dom.client.ClickHandler
com.extjs.gxt.ui.client.event.KeyListener
com.google.gwt.event.dom.client.KeyUpEvent
com.extjs.gxt.ui.client.event.ComponentEvent
com.google.gwt.event.dom.client.KeyUpHandler
We then needed to redefine the controls. In the GWT example, all the code sits inside the
onModuleLoad
method and makes use of inner classes. However, due to the way listeners are implemented in GXT, we lose some of the flexibility that enables this. Instead, we had to define the controls as private members as follows:private final Button sendButton = new Button("Send"); private final TextField<String> nameField = new TextField<String>(); private final Dialog dialogBox = new Dialog(); private final Label textToServerLabel = new Label(); private final HTML serverResponseLabel = new HTML();
There are differences in syntax between the GXT and GWT methods. Although the GXT controls are similar to GWT controls, there are a number of differences. Firstly, there are many small differences on the methods of the controls between GWT and GXT. Here are the ones we see in this example:
GXT
GWT
TextField.setValue()
TextBox.setText()
TextField.focus()
TextBox.setFocus(true)
DialogBox.setHeading()
DialogBox.setText()
DialogBox.setAnimCollapse(true)
DialogBox.setAnimationEnabled(true)
VerticalPanel .setHorizontalAlign(HorizontalAlignment.RIGHT);
VerticalPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT)
Another difference that is important is that while GWT now uses event handlers for events such as clicking on a button, GXT uses event listeners similar to the earlier version of GWT. However, in this case, the actual code is very similar.
Here is how you implement the close button click event in GWT using a click handler:
closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { dialogBox.hide(); sendButton.setEnabled(true); sendButton.setFocus(true); } });
Here is the same thing in GXT using a selection listener:
closeButton.addSelectionListener(new SelectionListener<ButtonEvent>() { public void componentSelected(ButtonEvent ce) { dialogBox.hide(); sendButton.setEnabled(true); sendButton.focus(); } });
We now have two classes: the original GWT
FirstApp
class and our newFirstGXTApp
class. To use theFirstGXTApp
, we need to change the application'sgwt.xml
module file to use theFirstGXTApp
instead ofFirstApp
.Open
FirstApp.gxt.xml
and change the entry point element from:<entry-point class='com.danielvaughan.firstapp.client.FirstApp' />
to:
<entry-point class='com.danielvaughan.firstapp.client.FirstGXTApp' />
Now when running the web application again, you will see a new version with GXT controls.
Hopefully, you now can see that using GXT is not vastly different from using GWT. It is also important to realize that there are some subtle differences. Over the coming chapters, we will show that there are many great features in GXT that go far beyond the basics provided by GWT.
What JavaScript library is GXT closely related to?
Which GXT alternative wraps the Smart Client JavaScript library?
Which GXT alternative does most of the work on the server?
Which GXT alternative has a name and appearance that is easily confused with Ext GWT?
What is the name of the company that develops GXT?
What is the name of the GXT Java library file?
What is the license of GXT?
In what file must you inherit the GXT module?
Where must you include a reference to the GXT CSS?
Where must you copy the
gxt.jar
library file?