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
You can create a new anonymous
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.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 thebutton.addClickListener(new ClickListener() { public void onClick(Widget sender) { processData(sender.getText()); reportData(sender.getText()); logResults(); } });
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.
With this approach, if we want to add apublic class MainPanel extends Composite implements ClickListener{ private Button button=new Button("Button"); public MainPanel(){ //... button.addClickListener(this); } public void onClick(Widget sender){ if(sender==button){ processData(sender.getText()); reportData(sender.getText()); logResults(); } } }
HTML widget link that implements the same function, we would just have to add the lineinlink.addClickListener(this);
MainPanel() and modify one line in the onClick(Widget sender)methodGiven Below is a listing of GWT Event listeners for handling various events:if(sender==button||sender==link)
| Listener | Event notifications | Methods Defined |
| HistoryListener | Changes to Browser History | onHistoryChanged(String historyToken) |
| WindowCloseListener | Window Closure Events | onWindowClosed() onWindowClosing() |
| WindowResizeListener | Window Resizing Events | onWindowResized(int width, int height) |
| ChangeListener | fires when a widget changes | onChange(Widget sender) |
| ClickListener | fires when a widget receives a 'click' | onClick(Widget sender) |
| FormHandler | fires on receiving Form events from the FormPanel to which it is attached | onSubmit(FormSubmitEvent event) onSubmitComplete( FormSubmitCompleteEvent event) |
| FocusListener | Focus Events | onFocus(Widget sender) onLostFocus(Widget sender) |
| KeyBoardListener | Keyboard Events | onKeyDown(Widget sender, char keycode, int modifiers) onKeyPress(Widget sender, char keycode, int modifiers) onKeyUp(Widget sender, char keycode, int modifiers) |
| LoadListener | listens to 'load' event of a widget | onLoad(Widget Sender) onError(Widget Sender) |
| MouseListener | 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) |
| PopupListener | listens to the events of a PopupPanel widget | onPopupClosed(PopupPanel sender, boolean autoClose) |
| ScrollListener | listens to Scroll events | onScroll(Widget widget, int scrollLeft, int scrollTop) |
| TableListener | listens to events pertaining to a Table widget | onCellClicked( SourcesTableEvents sources, int row, int cell) |
| TabListener | listens to the events of a Tab Widget | onBeforeTabSelected( SourcesTabEvents sender, int tabIndex) onTabSelected( SourcesTabEvents sender, int tabIndex) |
| TreeListener | 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 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.


0 comments:
Post a Comment