5
votes

If i have a GWT composite widget with three text boxes like for SSN, and i need to fire change event only when focus is lost from the widget as a whole, not from individual text boxes how to go about doing that?

3

3 Answers

1
votes

If you want just the event when your whole widget loses focus (not the text boxes), then make the top level of your widget be a FocusPanel, and expose the events that it gives you.

0
votes

You need to implement the Observer Pattern on your composite, and trigger a new notification everytime:

  • the focus is lost on a specific text box AND
  • the focus was not transferred to any of the other text boxes.
0
votes

Couldn't you use a timer? On lost focus from a text box, start a 5ms (or something small) timer that when it hits, will check focus on all 3 TextBox instances. If none have focus, then you manually notify your observers. If one has focus, do nothing.

Put this in your Composite class:

private Map<Widget, Boolean> m_hasFocus = new HashMap<Widget, Boolean>();

And then add this to each one of your TextBox instances:

new FocusListener() {
  public void onFocus(Widget sender) {
    m_hasFocus.put(sender, Boolean.TRUE);
  }

  public void onLostFocus(Widget sender) {
    m_hasFocus.put(sender, Boolean.FALSE);
    new Timer() {
      public void run() {
        for (Boolean bool : m_hasFocus.values()) {
          if (bool) { return; }
        }
        notifyObservers();
      }
    };
  }
};