enable/disable widgets?

3,320 views
Skip to first unread message

Magnus

unread,
Jun 17, 2010, 1:27:26 PM6/17/10
to Google Web Toolkit
Hi,

how can I enable/disable a widget (TextBox, Button, etc.)?

I would like to iterate all Widgets of a form and set this status. I
found no appropriate methods in the Widget class...

Magnus

Stefan Bachert

unread,
Jun 18, 2010, 10:38:49 AM6/18/10
to Google Web Toolkit
Hi Magnus,

I agree with you, there should be such a method at UiObject, but
actually it it not.
You may write an enhancement request

Stefan Bachert
http://gwtworld.de

Trevor Skaife

unread,
Jun 18, 2010, 11:09:48 AM6/18/10
to Google Web Toolkit
The setEnabled method is in the FocusWidget which I'm assuming all of
your input widgets extend. So just cast all of the widgets in your
form to a FocusWidget and then you can enable/disable them.

Trevor

On Jun 18, 9:38 am, Stefan Bachert <stefanbach...@yahoo.de> wrote:
> Hi Magnus,
>
> I agree with you, there should be such a method at UiObject, but
> actually it it not.
> You may write an enhancement request
>
> Stefan Bacherthttp://gwtworld.de

Stefan Bachert

unread,
Jun 19, 2010, 10:33:04 AM6/19/10
to Google Web Toolkit
Hi,

that is true. However, in any good gui you need sometimes to disable a
label or a menu-item, too.
Supplying FocusWidget with as enabling/disabling method is just too
less. The cause is that <input> has an html-attribute called
"disabled".

The current widget api of GWT is too short sighted.
It is driven by what is offered by the html tag rather than what is
needed by the gui-developers.

Stefan Bachert
http://gwtworld.de

Jaroslav Záruba

unread,
Jun 19, 2010, 10:52:40 AM6/19/10
to google-we...@googlegroups.com
I do agree, Stefan. It should be made clear what exactly does 'disabled' mean for a Widget. Should disabled Widget 'ignore' click events completely?


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


André Moraes

unread,
Jun 19, 2010, 11:50:49 AM6/19/10
to Google Web Toolkit
It's more like a workaround, but you could:

- Remove the ClickHandlers for the widget (easy if the widget is
private under a composite)
- use addStyleName, removeStyleName to set the css class of the
disabled widgets.

Using the Composite base class if very nice and you can use all the
widget power and just display the interface that your custom widget
need.
I usually make all my app-wide components (menus, toolbars, status
bar, etc...) be inside a custom widget so I can change many aspects of
the look/behavior of the component without worrying about breaking the
interface of the component

http://code.google.com/intl/webtoolkit/webtoolkit/doc/latest/DevGuideUiCustomWidgets.html

This workaround will be ok for the menus and labels you pointed.

On 19 jun, 11:52, Jaroslav Záruba <jaroslav.zar...@gmail.com> wrote:
> I do agree, Stefan. It should be made clear what exactly does 'disabled'
> mean for a Widget. Should disabled Widget 'ignore' click events completely?
>
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .

Jaroslav Záruba

unread,
Jun 19, 2010, 12:31:04 PM6/19/10
to google-we...@googlegroups.com
Well, there's always some work-around, but GWT is supposed to help us from those.

2010/6/19 André Moraes <and...@gmail.com>
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Magnus

unread,
Jun 24, 2010, 1:08:51 AM6/24/10
to Google Web Toolkit
Hello,

I actually needed the functionality in the widget class to let my own
"Form" class enable/disable all its child widgets at the same time.

However, I made this workaround and it's ok for me at the moment:

public void setEnabled (boolean e)
{
Iterator<Widget> itr = grd.iterator (); // grd is a FlexTable which
contains my form controls

while (itr.hasNext ())
{
Widget w = itr.next ();

if (w instanceof TextBox)
{
TextBox t = (TextBox) w;
t.setEnabled (e);
}

if (w instanceof PasswordTextBox)
{
PasswordTextBox t = (PasswordTextBox) w;
t.setEnabled (e);
}

...
}
}

Vitrums

unread,
Nov 24, 2012, 6:01:29 PM11/24/12
to google-we...@googlegroups.com
Found difficulties in some versions of Opera with exercising CSS properties like .myclass[disabled] {...}. Also I haven't found any solution how to sink events for disabled widgets. Seems like they are totally non-interactive (however :hover property still does some effect) speaking in terms of standard events, available for handling. Manually implemented enabled/disabled state has been the only suitable solution, which I'd found till present.

There's one good article about preview native event. In a nutshell you may examine what current event is about and cancel it before browser starts any propagation. Also I found this article quite useful to keep up on date with modern API. Here's a very concise piece of code:

In some singleton filter class (e.g. NativePreviewHandlerForHasEnabled):
*************************
private final Map<Element, HasEnabled> elementToWidgetMap = new HashMap<Element, HasEnabled>();

private NativePreviewHandlerForHasEnabled() {
assert instance == null : "Only one instance is allowed.";
elementToWidgetMap = new HashMap<Element, HasEnabled>();

Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
           public void onPreviewNativeEvent(NativePreviewEvent pEvent) {
               final Element target = pEvent.getNativeEvent().getEventTarget().cast();

               HasEnabled widget = elementToWidgetMap.get(target);
               if (widget != null && !widget.isEnabled()) {
               
                // Let it work, cause we may need to e.g. hide tool tip when mouse is out
                if (pEvent.getTypeInt() != Event.ONMOUSEOUT) {
                pEvent.cancel();
                }
               }
           }
       }); 
}

public <T extends Widget & HasEnabled> void registerWidget(T widget) {
elementToWidgetMap.put(widget.getElement(), widget);
}
*************************

For our widget:

@Override
public void setEnabled(boolean enabled) {
this.enabled = enabled;
// super.setEnabled(enabled); // don't do this!
}

It's not perfect. Map can grow up fast depending on scale of your app. To enhance the performance store HandlerRegistration objects upon Event.addNativePreviewHandler call for later disposing, when the time comes.

Patrick Tucker

unread,
Nov 26, 2012, 8:09:34 AM11/26/12
to google-we...@googlegroups.com
You really should take a look at the "Known Implementing Classes":
Reply all
Reply to author
Forward
0 new messages