Monday, November 22, 2010

GWT Event listeners

What is GWT Event Listeners?
Answer : GWT defines the event listeners that you can attach to a widget to monitor browser
 events. Event Listeners are just interfaces ,that you can implement on your widget.

Question : How to trap mouse click events?
Answer :  your widget must implement clickListener Interface and defines the method
 onClick(widget sender)  the code inside this method will triggered whenever user clicks on that
widget.

Question : How to Implement clickListener Interface ?
Answer : Can be implemented anonymously  like below :
          button.addClickListener(new ClickListener() {
     public void onClick(Widget sender) {
     processData(sender.getText());
     reportData(sender.getText());
      logResults();
     }
   });
Note: The button widget add methods addClickListener and attaches object
ClickListener to it.

Question : What are the disadvantages of above anonymous implementation?
Answer : code should b repeat on each object. 

Question : What are the other way of implementing listener?
answer  : Put the listener implementation in the class.

Example : Public class MainPanel extends composite implements
           ClickListener {
               private Button mybutton = new Button("Click");
               MainPanel(){
              ......
               mybutton.addClickListener(this);
               }
             public void onClick(Widget sender){
              if(sender == button){
               //Do implementation for button;
             }
           }
NOTE: Implement the listeners in container widget to handle the events of
       Contained widget.
Question : What can i do if i want add clicklistener in another link.
Answer : Simple ,
link.addClickListener(this);
if(sender == link || sender == button)
{ Do somethng this } 
Note: all Event Listener are available to all Widgets.
Question : What is composite ?
Answer : Combination of two widgets.
Question : How will you implement the keyUpActionListener in textField?
Answer : public class KeyListener 
{
TextField tf = new TextField();
tf.addKeyUpHandler(new keyUp(this));
}
--------------------------------------------------------
public class keyUp implements KeyUpHandler {
private KeyListener app ;
keyUp(KeyListener app)
{ this.app = app}
public void onKeyUp(KeyUpEvent event){
...................
}

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...