Sunday, July 17, 2011

Layers in Java

Possibilities of Using Layers: The layers of a panel can be used to create more better interactive systems. We can use the glass-panes and layers to show some extra information on the top of other controls while blocking mouse events on the controls. We can create interactive tutorials and manuals with animations in the middle of some process. Similarly, layered panes are also used for selected object highlighting, focusing or showing detail information in pop up like window.

The dragging and dropping can be made more user friendly by use of layered panes for showing the thumbnail of dragged component on the mouse pointer. Since we do not need more than one layer for this, so glass pane is sufficient to accomplish this task.

How to use Layers:
Glass Pane:For glass pane, we create component for glass pane and then set this component as glass pane.
CustomGlassPane glassPane=new CustomGlassPane();
While creating glass panes, we generally need to disable input events inside the glass pane. So, we need to implement disable the listeners in the glass pane component. Initially glass pane component is invisible, so when required we make the glass pane visible.

Layered Panes:For layered panes, we define the layered component and then set this component as layered pane as follows:
LayeredComponent component=new LayeredComponent();
JlayeredPane layeredPane=getRootPane().getLayeredPane();

The depth defines where in the z-order the layer to be displayed. We need to define the layout of the layered pane because there will not be any default layout for this.

Glass Pane Limitations:
We can not create multiple glass panes because sometimes we may require multiple glass panes to be implemented to achieve our goal.
(ii) Glass pane always cover the entire frame, so if we need to cover only part of the frame, then its not possible.

Layered Panes Limitations:
The layered panes remove the trade offs of glass pane because we can have multiple layers and layers can be set for each child container on the frame. However, we need to create our own layout for the layered component. Although we can use OverlayLayout but that is very simple to meet our requirements. The main disadvantage of layered panes is the complexity of implementation; so many designers prefer to select layout manager to fulfill the requirement of depth-wise positioning of components.

No comments:

Post a Comment