## com.google.gwt.user.client.ui
Class Widget

    java.lang.Object
      com.google.gwt.user.client.ui.UIObject
          com.google.gwt.user.client.ui.Widget

All Implemented Interfaces:
  HasHandlers, EventListener

Direct Known Subclasses:
  Composite, FileUpload, FocusWidget, Frame, Hidden, Hyperlink, Image, Label, MenuBar, Panel, Tree

    public class Widget

extends UIObject
implements EventListener, HasHandlers

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`.

Nested Class Summary

Nested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
`UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled`

Field Summary

Fields inherited from class com.google.gwt.user.client.ui.UIObject
`DEBUG_ID_PREFIX`

Constructor Summary
`Widget()`

Method Summary

`protected

`<H extends EventHandler> HandlerRegistration`

`

`addDomHandler(H handler, DomEvent.Type<H> type)`
Adds a native event handler to the widget and sinks the corresponding native event.

`protected

`<H extends EventHandler> HandlerRegistration`

`

`addHandler(H handler, GwtEvent.Type<H> type)`
Adds this handler to the widget.

`protected void` | `delegateEvent(Widget target, GwtEvent<?> event)` Fires an event on a child widget.
`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.
`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.
`void` | `fireEvent(GwtEvent<?> event)` Fires the given event to all the appropriate handlers.
`protected int` | `getHandlerCount(GwtEvent.Type<?> type)` Gets the number of handlers listening to the event type.
`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).
`protected boolean` | `isOrWasAttached()` Has this widget ever been attached?
`protected void` | `onAttach()` This method is called when a widget is attached to the browser's document.
`void` | `onBrowserEvent(Event event)` Fired whenever a browser event is received.
`protected void` | `onDetach()` This method is called when a widget is detached from the browser's document.
`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.
`void` | `removeFromParent()` Removes this widget from its parent widget, if one exists.
`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.

Methods inherited from class com.google.gwt.user.client.ui.UIObject

`addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, onEnsureDebugId, removeStyleDependentName, removeStyleName, setElement, setElement, setHeight, setPixelSize, setSize, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, toString, unsinkEvents`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### Widget

    public Widget()

Method Detail

### fireEvent

    public void fireEvent(GwtEvent<?> event)

  Description copied from interface: `HasHandlers`
  Fires the given event to all the appropriate handlers.

  Specified by:
    `fireEvent` in interface `HasHandlers`
  Parameters:
    `event` - the event to be fired

### getParent

    public Widget getParent()

  Gets this widget's parent panel.

  Returns:
    the widget's parent panel

### isAttached

    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

### onBrowserEvent

    public void onBrowserEvent(Event event)

  Description copied from interface: `EventListener`
  Fired whenever a browser event is received.

  Specified by:
    `onBrowserEvent` in interface `EventListener`
  Parameters:
    `event` - the event received

### removeFromParent

    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 `RootPanel.detachOnWindowClose(Widget)`), it will be removed from the detached immediately. This makes it possible for Composites and Panels to adopt root widgets.

  Throws:
    `java.lang.IllegalStateException` - if this widget's parent does not support removal (e.g. `Composite`)

### sinkEvents

    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);
        }
      }

  Overrides:
    `sinkEvents` in class `UIObject`
  Parameters:
    `eventBitsToAdd` - a bitfield representing the set of events to be added to this element's event set
  See Also:
    `Event`

### addDomHandler

    protected final <H extends EventHandler> HandlerRegistration addDomHandler(H handler,
                                                                               DomEvent.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.

  Type Parameters:
    `H` - the type of handler to add
  Parameters:
    `type` - the event key
    `handler` - the handler
  Returns:
    `HandlerRegistration` used to remove the handler

### addHandler

    protected final <H extends EventHandler> HandlerRegistration addHandler(H handler,
                                                                            GwtEvent.Type<H> type)

  Adds this handler to the widget.

  Type Parameters:
    `H` - the type of handler to add
  Parameters:
    `type` - the event type
    `handler` - the handler
  Returns:
    `HandlerRegistration` used to remove the handler

### delegateEvent

    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:
    `event` - the event
    `target` - fire the event on the given target

### doAttachChildren

    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:
    `onAttach()`

### doDetachChildren

    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:
    `onDetach()`

### getHandlerCount

    protected int getHandlerCount(GwtEvent.Type<?> type)

  Gets the number of handlers listening to the event type.

  Parameters:
    `type` - the event type
  Returns:
    the number of registered handlers

### isOrWasAttached

    protected final boolean isOrWasAttached()

  Has this widget ever been attached?

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

### onAttach

    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.

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

  Throws:
    `java.lang.IllegalStateException` - if this widget is already attached

### onDetach

    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.

  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:
    `java.lang.IllegalStateException` - if this widget is already detached

### onLoad

    protected void onLoad()

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

### onUnload

    protected void onUnload()

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