Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no isFocused() in GWT?

Tags:

gwt

In a programming scenario, I needed to check if my GWT textbox was focused or not. I ended up adding a boolean and a pair of Focus & BlurHandler to manually keep the focus state which makes me wonder why is there no such method that returns if a focusable component is focused in gwt?

like image 636
pistolPanties Avatar asked Feb 02 '23 13:02

pistolPanties


2 Answers

Because there wasn't any cross-browser way of doing it until a few years ago (Firefox 3, Safari 4, to point at the last players in the game having added support document.activeElement).

GWT still officially supports [1] Safari 3 (I believe Safari 2 support has been deprecated) and maybe even Firefox 2 (no DevMode plugin, but that doesn't mean the browser isn't supported: Opera is supported but has no DevMode either), so it's not possible to provide such a feature that would work in all supported browsers.

Last, but not least, I think no one ever filed a request for enhancement in the issue tracker (I couldn't find any at least); and as you said, you can already do it today using FocusHandler/BlurHandler (which works cross-browser).

[1] http://code.google.com/webtoolkit/doc/latest/FAQ_GettingStarted.html#What_browsers_does_GWT_support? I believe that page is a bit out of date, as it still lists Firefox 1.0, whose support (user.agent=gecko, vs. gecko1_8) has been removed in GWT 2.1.0, and doesn't list IE9, whose support has been added in GWT 2.3.0, and last but not least, I believe only the latest version of Opera is supported, whereas the list talks about Opera 9.

like image 65
Thomas Broyer Avatar answered Feb 05 '23 04:02

Thomas Broyer


To find which widget has focus, I don't know whether you have solution already. As a novice to GWT , I propose my solution to share:

  1. Declare private field in the object, like 'focusedWidget' step 1

  2. Create focus handler for the widget, here's class TextBox. In OnFocus block, just assign the widget to 'focusedWidget'. You can add this kind of event to every widget that can be focused. ![step 2][2]

  3. That's all. Every widget you tied the focus event will set itself to 'focusedWidget' everytime it is focused. We can then use 'focusedWidget' to determind which current widget is focused. ![step 3][3]

I test it in JUnit, it works! see image of snippet here Hope that help.

like image 36
Wutikrai Avatar answered Feb 05 '23 02:02

Wutikrai