public class

Widget

extends UIObject
implements HasAttachHandlers EventListener IsWidget
java.lang.Object
   ↳ com.google.gwt.user.client.ui.UIObject
     ↳ com.google.gwt.user.client.ui.Widget
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

The base class for the majority of user-interface objects. Widget adds support for receiving events from the browser and being added directly to panels.

Summary

[Expand]
Inherited Constants
From class com.google.gwt.user.client.ui.UIObject
Public Constructors
Widget()
Public Methods
HandlerRegistration addAttachHandler(AttachEvent.Handler handler)
Adds an AttachEvent handler.
final <H extends EventHandler> HandlerRegistration addDomHandler(H handler, Type<H> type)
Adds a native event handler to the widget and sinks the corresponding native event.
final <H extends EventHandler> HandlerRegistration addHandler(H handler, Type<H> type)
Adds this handler to the widget.
Widget asWidget()
Returns the Widget aspect of the receiver.
static Widget asWidgetOrNull(IsWidget w)
This convenience method makes a null-safe call to asWidget().
void fireEvent(GwtEvent<?> event)
Fires the given event to the handlers listening to the event's type.
Object getLayoutData()
Gets the panel-defined layout data associated with this widget.
Widget getParent()
Gets this widget's parent panel.
boolean isAttached()
Determines whether this widget is currently attached to the browser's document (i.e., there is an unbroken chain of widgets between this widget and the underlying browser document).
void onBrowserEvent(Event event)
Fired whenever a browser event is received.
void removeFromParent()
Removes this widget from its parent widget, if one exists.
void setLayoutData(Object layoutData)
Sets the panel-defined layout data associated with this widget.
void sinkEvents(int eventBitsToAdd)
Overridden to defer the call to super.sinkEvents until the first time this widget is attached to the dom, as a performance enhancement.
Protected Methods
HandlerManager createHandlerManager()
Creates the HandlerManager used by this Widget.
void delegateEvent(Widget target, GwtEvent<?> event)
Fires an event on a child widget.
void doAttachChildren()
If a widget contains one or more child widgets that are not in the logical widget hierarchy (the child is physically connected only on the DOM level), it must override this method and call onAttach() for each of its child widgets.
void doDetachChildren()
If a widget contains one or more child widgets that are not in the logical widget hierarchy (the child is physically connected only on the DOM level), it must override this method and call onDetach() for each of its child widgets.
int getHandlerCount(Type<?> type)
Gets the number of handlers listening to the event type.
final boolean isOrWasAttached()
Has this widget ever been attached?
void onAttach()

This method is called when a widget is attached to the browser's document.

void onDetach()

This method is called when a widget is detached from the browser's document.

void onLoad()
This method is called immediately after a widget becomes attached to the browser's document.
void onUnload()
This method is called immediately before a widget will be detached from the browser's document.
[Expand]
Inherited Methods
From class com.google.gwt.user.client.ui.UIObject
From class java.lang.Object
From interface com.google.gwt.event.logical.shared.HasAttachHandlers
From interface com.google.gwt.event.shared.HasHandlers
From interface com.google.gwt.user.client.EventListener
From interface com.google.gwt.user.client.ui.IsWidget

Public Constructors

public Widget ()

Public Methods

public HandlerRegistration addAttachHandler (AttachEvent.Handler handler)

Adds an AttachEvent handler.

Parameters
handler the handler
Returns
  • the handler registration

public final HandlerRegistration addDomHandler (H handler, Type<H> type)

Adds a native event handler to the widget and sinks the corresponding native event. If you do not want to sink the native event, use the generic addHandler method instead.

Parameters
handler the handler
type the event key
Returns

public final HandlerRegistration addHandler (H handler, Type<H> type)

Adds this handler to the widget.

Parameters
handler the handler
type the event type
Returns

public Widget asWidget ()

Returns the Widget aspect of the receiver.

public static Widget asWidgetOrNull (IsWidget w)

This convenience method makes a null-safe call to asWidget().

Returns
  • the widget aspect, or null if w is null

public void fireEvent (GwtEvent<?> event)

Fires the given event to the handlers listening to the event's type.

Any exceptions thrown by handlers will be bundled into a UmbrellaException and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.

Parameters
event the event

public Object getLayoutData ()

Gets the panel-defined layout data associated with this widget.

Returns
  • the widget's layout data

public Widget getParent ()

Gets this widget's parent panel.

Returns
  • the widget's parent panel

public boolean isAttached ()

Determines whether this widget is currently attached to the browser's document (i.e., there is an unbroken chain of widgets between this widget and the underlying browser document).

Returns
  • true if the widget is attached

public void onBrowserEvent (Event event)

Fired whenever a browser event is received.

Parameters
event the event received

public void removeFromParent ()

Removes this widget from its parent widget, if one exists.

If it has no parent, this method does nothing. If it is a "root" widget (meaning it's been added to the detach list via detachOnWindowClose(Widget)), it will be removed from the detached immediately. This makes it possible for Composites and Panels to adopt root widgets.

Throws
IllegalStateException if this widget's parent does not support removal (e.g. Composite)

public void setLayoutData (Object layoutData)

Sets the panel-defined layout data associated with this widget. Only the panel that currently contains a widget should ever set this value. It serves as a place to store layout bookkeeping data associated with a widget.

Parameters
layoutData the widget's layout data

public void sinkEvents (int eventBitsToAdd)

Overridden to defer the call to super.sinkEvents until the first time this widget is attached to the dom, as a performance enhancement. Subclasses wishing to customize sinkEvents can preserve this deferred sink behavior by putting their implementation behind a check of isOrWasAttached():

 @Override
 public void sinkEvents(int eventBitsToAdd) {
   if (isOrWasAttached()) {
     /* customized sink code goes here */
   } else {
     super.sinkEvents(eventBitsToAdd);
  }
} 

Parameters
eventBitsToAdd a bitfield representing the set of events to be added to this element's event set

Protected Methods

protected HandlerManager createHandlerManager ()

Creates the HandlerManager used by this Widget. You can override this method to create a custom HandlerManager.

Returns

protected void delegateEvent (Widget target, GwtEvent<?> event)

Fires an event on a child widget. Used to delegate the handling of an event from one widget to another.

Parameters
target fire the event on the given target
event the event

protected void doAttachChildren ()

If a widget contains one or more child widgets that are not in the logical widget hierarchy (the child is physically connected only on the DOM level), it must override this method and call onAttach() for each of its child widgets.

See Also

protected void doDetachChildren ()

If a widget contains one or more child widgets that are not in the logical widget hierarchy (the child is physically connected only on the DOM level), it must override this method and call onDetach() for each of its child widgets.

See Also

protected int getHandlerCount (Type<?> type)

Gets the number of handlers listening to the event type.

Parameters
type the event type
Returns
  • the number of registered handlers

protected final boolean isOrWasAttached ()

Has this widget ever been attached?

Returns
  • true if this widget ever been attached to the DOM, false otherwise

protected void onAttach ()

This method is called when a widget is attached to the browser's document. To receive notification after a Widget has been added to the document, override the onLoad() method or use addAttachHandler(AttachEvent.Handler).

It is strongly recommended that you override onLoad() or doAttachChildren() instead of this method to avoid inconsistencies between logical and physical attachment states.

Subclasses that override this method must call super.onAttach() to ensure that the Widget has been attached to its underlying Element.

Throws
IllegalStateException if this widget is already attached

protected void onDetach ()

This method is called when a widget is detached from the browser's document. To receive notification before a Widget is removed from the document, override the onUnload() method or use addAttachHandler(AttachEvent.Handler).

It is strongly recommended that you override onUnload() or doDetachChildren() instead of this method to avoid inconsistencies between logical and physical attachment states.

Subclasses that override this method must call super.onDetach() to ensure that the Widget has been detached from the underlying Element. Failure to do so will result in application memory leaks due to circular references between DOM Elements and JavaScript objects.

Throws
IllegalStateException if this widget is already detached

protected void onLoad ()

This method is called immediately after a widget becomes attached to the browser's document.

protected void onUnload ()

This method is called immediately before a widget will be detached from the browser's document.