Welcome to my blog, hope you enjoy reading

Monday, 15 April 2013

GWT event listeners

GWT event listeners

GWT defines event listeners that you can attach to your widgets to monitor browser events. Event Listeners are just interfaces that you can implement in your widget. If you want to trap mouse click events, then your widget should implement the ClickListener interface and define theonClick(Widget sender) method. The code inside this method will be fired every time the user clicks on your widget.

You can create a new anonymous ClickListener every time you want to assign it to a widget as given in the Hello gwt example.
button.addClickListener(new ClickListener() {
 public void onClick(Widget sender) {
But this results in localized code and lesser code reusability. Let us say we want to add another widget (a HTML link), which has to run the same code as in the onClick() method. We would be using the addClickListenermethod for this link and would be rewriting the same code again separately. And if we need to add another method in the onClick() method code between processData() and reportData() we will have to change each instance of onClick().

Alternatively, we can implement the listeners in the container widget to handle the events of contained widgets as shown below, making the code more reusable.
public class MainPanel extends Composite 
 implements ClickListener{

 private Button button=new Button("Button");
 public MainPanel(){

 public void onClick(Widget sender){
With this approach, if we want to add a HTML widget link that implements the same function, we would just have to add the line
in MainPanel() and modify one line in the onClick(Widget sender)method
Given Below is a listing of GWT Event listeners for handling various events:

ListenerEvent notificationsMethods Defined
HistoryListenerChanges to Browser HistoryonHistoryChanged(String historyToken)
WindowCloseListenerWindow Closure EventsonWindowClosed()
WindowResizeListenerWindow Resizing EventsonWindowResized(int width, int height)
ChangeListenerfires when a widget changesonChange(Widget sender)
ClickListenerfires when a widget receives a 'click'onClick(Widget sender)
FormHandlerfires on receiving Form events from the FormPanel to which it is attachedonSubmit(FormSubmitEvent event)
onSubmitComplete( FormSubmitCompleteEvent event)
FocusListenerFocus EventsonFocus(Widget sender)
onLostFocus(Widget sender)
KeyBoardListenerKeyboard EventsonKeyDown(Widget sender, char keycode, int modifiers)
onKeyPress(Widget sender, char keycode, int modifiers)
onKeyUp(Widget sender, char keycode, int modifiers)
LoadListenerlistens to 'load' event of a widgetonLoad(Widget Sender)
onError(Widget Sender)
MouseListenerlistens to mouse events of a widgetonMouseEnter(Widget sender)
onMouseLeave(Widget sender)
onMouseDown(Widget sender, int x, int y)
onMouseUp(Widget sender, int x, int y)
onMouseMove(Widget sender, int x, int y)
PopupListenerlistens to the events of a PopupPanel widgetonPopupClosed(PopupPanel sender, boolean autoClose)
ScrollListenerlistens to Scroll eventsonScroll(Widget widget, int scrollLeft, int scrollTop)
TableListenerlistens to events pertaining to a Table widgetonCellClicked( SourcesTableEvents sources, int row, int cell)
TabListenerlistens to the events of a Tab WidgetonBeforeTabSelected( SourcesTabEvents sender, int tabIndex)
onTabSelected( SourcesTabEvents sender, int tabIndex)
TreeListenerlistens to the events of a Tree WidgetonTreeItemSelected( TreeItem item)
onTreeItemChanged( TreeItem item)

The Button widget defines the addClickListener method which attaches a ClickListener object to it. Similarly, the Tree widget defines theaddTreeListener method. Not all event listeners are available for all widgets - for example, the addMouseListener method is not defined for aDockPanel Widget. One way to add the MouseListener functionality to thisDockPanel is to create a composite that includes a FocusPanel widget (which supports addMouseListener) and wrap the DockPanel widget within it.