OverlayContainer is a WidgetContainer which overlays its children on top of each other. Every child
Widget is sized to fill the entire container, and the preferred size of the container is equal to the
largest preferred size of any of its children.
OverlayContainers are generally used to switch between several different Widgets which all appear in the
same position. When used this way, only one of the Widgets will be visible at any time. The
setVisibleChild()
method provides an easy way to
switch between the child Widgets by showing one and hiding all the others.
Another use of OverlayContainer is to place a partially transparent Widget in front of
another one. This allows an "overlay" to be displayed at a particular position, independent of the
positions of other Widgets in the window. An OverlayContainer can also be used to block mouse events
from reaching a Widget. Mouse events are always delivered to the topmost visible Widget that has requested
to receive them. Suppose that
mainWidget
is a Widget (possibly a WidgetContainer with
many children). The following code will block all mouse events from reaching it and its children:
CustomWidget blocker = new CustomWidget();
blocker.setOpaque(false);
blocker.addEventLink(WidgetMouseEvent.class, new Object() {
void processEvent()
{
// Ignore the event.
}
});
OverlayContainer overlay = new OverlayContainer();
overlay.add(mainWidget);
overlay.add(blocker);
You can then turn event blocking on or off by calling
blocker.setVisible(true)
or
blocker.setVisible(false)
. Note that we have added an event link to
blocker
which listens for mouse events. If you do not do this, it will ignore mouse events and let them pass
through to the Widget underneath.
In addition to the event types generated by all Widgets, OverlayContainers generate the following
event types:
add
public void add(Widget widget)
Add a Widget to this container. The new Widget will be placed in front of all other Widgets in the
container (at index 0).
widget
- the Widget to add
add
public void add(Widget widget,
int index)
Add a Widget to this container.
widget
- the Widget to addindex
- the index of the Widget within the container. Widgets with lower indices are displayed
in front of ones with higher indices.
getChild
public Widget getChild(int i)
Get the i'th child of this container.
getChildIndex
public int getChildIndex(Widget widget)
Get the index of a particular Widget.
widget
- the Widget to locate
- the index of the Widget within this container
getMinimumSize
public Dimension getMinimumSize()
Get the smallest size at which this Widget can reasonably be drawn. When a WidgetContainer lays out
its contents, it will attempt never to make this Widget smaller than its minimum size.
- getMinimumSize in interface Widget
getPreferredSize
public Dimension getPreferredSize()
Get the preferred size at which this Widget will look best. When a WidgetContainer lays out
its contents, it will attempt to make this Widget as close as possible to its preferred size.
- getPreferredSize in interface Widget
layoutChildren
public void layoutChildren()
Layout the child Widgets. This may be invoked whenever something has changed (the size of this
WidgetContainer, the preferred size of one of its children, etc.) that causes the layout to no
longer be correct. If a child is itself a WidgetContainer, its layoutChildren() method will be
called in turn.
- layoutChildren in interface WidgetContainer
remove
public void remove(Widget widget)
Remove a child Widget from this container.
- remove in interface WidgetContainer
widget
- the Widget to remove
setVisibleChild
public void setVisibleChild(Widget child)
Set a particular child Widget to be visible, and all others to be not visible.
setVisibleChild
public void setVisibleChild(int i)
Set the i'th child Widget to be visible, and all others to be not visible.