Wednesday, July 15, 2009

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 the onClick(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 addClickListener method 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:

Event notifications
Methods Defined
Changes to Browser History
onHistoryChanged(String historyToken)
Window Closure Events
Window Resizing Events
onWindowResized(int width, int height)
fires when a widget changes
onChange(Widget sender)
fires when a widget receives a 'click'
onClick(Widget sender)
fires on receiving Form events from the FormPanel to which it is attached
onSubmit(FormSubmitEvent event)
onSubmitComplete( FormSubmitCompleteEvent event)
Focus Events
onFocus(Widget sender)
onLostFocus(Widget sender)
Keyboard Events
onKeyDown(Widget sender, char keycode, int modifiers)
onKeyPress(Widget sender, char keycode, int modifiers)
onKeyUp(Widget sender, char keycode, int modifiers)
listens to 'load' event of a widget
onLoad(Widget Sender)
onError(Widget Sender)
listens to mouse events of a widget
onMouseEnter(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)
listens to the events of a PopupPanel widget
onPopupClosed(PopupPanel sender, boolean autoClose)
listens to Scroll events
onScroll(Widget widget, int scrollLeft, int scrollTop)
listens to events pertaining to a Table widget
onCellClicked( SourcesTableEvents sources, int row, int cell)
listens to the events of a Tab Widget
onBeforeTabSelected( SourcesTabEvents sender, int tabIndex)
onTabSelected( SourcesTabEvents sender, int tabIndex)
listens to the events of a Tree Widget
onTreeItemSelected( TreeItem item)
onTreeItemChanged( TreeItem item)

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

1 comment:

Your Ad Here ]]>