views:

84

answers:

1

I'm implementing a custom widget extending Composite and implementing Focusable and HasFocusHandlers.

The widget contains two widgets in a panel that I wrap into a FocusPanel that I use to initialize the widget. My constructor looks something like that:

public CustomBox() {
    panel = new VerticalPanel();
    ...
    panel.add(caption);
    panel.add(icon);
    ...
    focusPanel = new FocusPanel(panel);
    initWidget(focusPanel);
}

I delegate the implementation of the Focusable and HasFocusHandlers in the focus panel, e.g:

@Override
public void setFocus(boolean focused) {
    focusPanel.setFocus(focused);
}

@Override
public void setTabIndex(int index) {
    focusPanel.setTabIndex(index);        
}

@Override
public HandlerRegistration addFocusHandler(FocusHandler handler) {
    return focusPanel.addFocusHandler(handler);
}   

And after that I can use setFocus(true) to set the focus in any of my objects, and setTabIndex() to set the tab index. Tab key also works as expected, but my problem is that I cannot handle focus events as the onFocus() method of the handlers added with addFocusHandler() are never called.

I know that the focus is changing because I follow the focus of the objets changing its style with :focus CSS selector.

Why are focus handlers never called?

A: 

My guess is that somewhere you have a block that prevents default event behavior. For example you get this if you put the call DOM.eventPreventDefault(event); in an override of the method onBrowserEvent(Event event). This would explain why the browser shows focus, because in such a case the focus would be set, but the handlers wouldn't be called.

Hilbrand