Roughian Examples Site Map - Examples - Tutorials

FocusListener


Version 1.0 onwards

Alerts You When Your Widget Gets Or Loses Focus



Sources



Notes


There are two events: onFocus() and onLostFocus(). Self-explanatory, really.


Code

If you are new to listeners, then this isn't the simplest of examples, I'd have a look at the Button example under GWT Widgets for that


class Demo extends Composite implements FocusListener
{
    TextBox textbox = new TextBox();

    public Demo()
    {
        initWidget(textbox);
        textbox.setWidth("100%");
        textbox.setText("Click Here");
        DOM.setStyleAttribute(textbox.getElement(), "textAlign", "center");
        textbox.addFocusListener(this);
    }
    public void onFocus(Widget sender)
    {
        textbox.setText("Got Focus - Click Outside This Box");
    }
    public void onLostFocus(Widget sender)
    {
        textbox.setText("Lost Focus - Click Here Again");
    }
}