GWT FocusPanel, tab key and focus handlers
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) {
开发者_JS百科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?
Handling focus events can be tricky and fickle. GWT should make this easy for you by adding hidden input fields into your FocusPanel DIV when needed. Here's an excerpt from the jQuery docs:
The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.
I don't know the solution to your problem, but my guess is that something very HTML/browser/dom related is messing up the event handling. For example, are either the caption
or icon
also a FocusPanel
? This might impact how focus events bubble up.
精彩评论