adding eventhandler to custon textbox field

94 views
Skip to first unread message

arian

unread,
Jun 13, 2012, 5:41:32 PM6/13/12
to google-we...@googlegroups.com
I have a custom TextBox that has a IndicatorTextBox.ui.xml file as well as  IndicatorTextBox.java file.
Ussually adding an evenhadler to a textbox is  simple.

@UiHandler("txtFirstName")
void onTxtFirstNameKeyUp(KeyUpEvent event) 
{
validateFields();
}

How would I add the handler if the txtFirstName was the custom textbox with label that I am adding to this page.?
So, in other words txtFirstnName is not @UiField TextBox txtFirstName but IndicatorTextField txtFirstName instead.

Sorry if this question seems a bit silly but still coming to grips with gwt and ui binder.

The   IndicatorTextBox.java file looks as follow

import com.google.gwt.core.client.GWT;


public class IndicatorTextField extends Composite implements HasText{
public interface Binder extends UiBinder<Widget, IndicatorTextField> {
}
private static final Binder binder = GWT.create(Binder.class);
public interface Style extends CssResource{
String textStyling();
String requiredInputLabel();
String colorNotValidated();
}
@UiField Style style;
@UiField Label label;
@UiField TextBox textBox;
public IndicatorTextField()
{
initWidget(binder.createAndBindUi(this));
}
public void setBackgroundValidateTextbox(boolean validated)
{
if(validated)
{
textBox.getElement().addClassName(style.colorNotValidated());
}
else
{
textBox.getElement().removeClassName(style.colorNotValidated());
}
}

@Override
public String getText() {
return label.getText();
}

@Override
public void setText(String text) {
label.setText(text);
}

regards
 

Jens

unread,
Jun 13, 2012, 6:16:24 PM6/13/12
to google-we...@googlegroups.com
Implement the HasXYZHandlers interfaces you need in your IndicatorTextField, e.g.:

public class IndicatorTextField extends Composite implements HasText, HasKeyUpHandlers, ...other handlers you need... {

  ....

  public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
    return addDomHandler(handler, KeyUpEvent.getType());
  }

}


Now you can use:

@UiHandler("yourIndicatorTextField")
void onKeyUp(KeyUpEvent event) {
  //your event code
}


-- J.

arian

unread,
Jun 14, 2012, 3:24:06 AM6/14/12
to google-we...@googlegroups.com
Hi, thanks for that.

However the event is not fired. Could I be missing something.

Jens

unread,
Jun 14, 2012, 5:50:10 AM6/14/12
to google-we...@googlegroups.com
Ooops, sorry my bad :) Somehow I assumed you want to fire events yourself using fireEvent() inside a composite. If you want to hook up your "internal" textbox then the easiest solution is to delegate to your "internal" textbox, e.g.:

public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
    return textbox.addKeyUpHandler(handler);
}

But in that case the KeyUpEvent.getSource() method will return the "internal" textbox and not your IndicatorTextField because the TextBox fired the event. You have to decide if you are fine with it. If you are not, I think you have to catch the KeyUpEvent from the textbox internally in your composite and then re-fire it with the corrected source.

-- J.

arian

unread,
Jun 14, 2012, 6:08:26 AM6/14/12
to google-we...@googlegroups.com
Thanks again.

Maybe I can post you what I have done and you can pinpoint what I am missing

IndicatorTextField.java

public class IndicatorTextField extends Composite implements HasText, HasKeyUpHandlers, HasBlurHandlers{

   
    public interface Binder extends UiBinder<Widget, IndicatorTextField> {
    }
   
    private static final Binder binder = GWT.create(Binder.class);
   
    public interface Style extends CssResource{
        String textStyling();
        String requiredInputLabel();
        String colorNotValidated();
       
       
    }
   
    @UiField Style style;
    @UiField Label label;
    @UiField TextBox textBox;
   
   
    public IndicatorTextField()
    {
        initWidget(binder.createAndBindUi(this));
    }
   
   

    @Override
    public String getText() {
       
        return label.getText();
    }

    @Override
    public void setText(String text) {
        label.setText(text);
       
    }


    @Override
    public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
       
        return textBox.addKeyUpHandler(handler);
        //return addDomHandler(handler, KeyUpEvent.getType());
    }
    @UiHandler("textBox")
    public void onKeyUp(KeyUpEvent event)
    {
       
        DomEvent.fireNativeEvent(event.getNativeEvent(), this);
    }
   
    @Override
    public HandlerRegistration addBlurHandler(BlurHandler handler) {
       
        return textBox.addBlurHandler(handler);
        //return addDomHandler(handler, BlurEvent.getType());
    }
    @UiHandler("textBox")
    public void onBlur(BlurEvent event)
    {
        DomEvent.fireNativeEvent(event.getNativeEvent(), this);
    }
}

This is the code in my main class where I am applying the textbox
@UiField IndicatorTextField txtFirstName;


@UiHandler("txtFirstName")
    void onKeyUp(KeyUpEvent event)
    {
        validateFields();       
    }
    @UiHandler("txtFirstName")
    void onBlur(BlurEvent event)
    {
        validateFields();
    }
}
this should then fire validateFields() but still not.

Arian


The class where I want to use the custom textbox
Reply all
Reply to author
Forward
0 new messages