mplayout
Class MultiPanelLayout

java.lang.Object
  |
  +--mplayout.MultiPanelLayout

public class MultiPanelLayout
extends java.lang.Object
implements java.awt.LayoutManager2

Lays out multiple panels containing components within a parent container. Maintains left and right alignment of all components in all panels.
This is the only layout that operates on multiple containers at once. The parent container has to be designated as the master. Calling layout on this container lays out all other dependent containers. Calling layout on a dependent container has no effect.

Steps to use:

  • Decide the screen grid size, lets say 80 by 24.
  • Create the parent container (frame, lets say).
  • Create one instance of MultiPanelLayout with the above mentioned parameters.
  • Set layout manager of the parent container to this class.
  • Lets say you have five panels to display in the frame, p1 to p5. The panels may overlap each other.
  • Decide the grid dimensions of the containers and of the components to be added within them.
  • Add the dependent containers (panels p1 to p5) to the parent container.
  • Add the components to their respective panels.
  • Note that a new instance of the constraint class MultiPanelLayoutConstraint has to be created for every component added. Unlike GridBagLayout, a new copy of the class is NOT created internally.

    You are free to use this code and to make modifications provided this notice is retained.

    If you found this useful, please add a note of acknowledgement to my guestbook. If you would like to report a bug or suggest some improvements, you are most welcome. I will be happy to help you use this piece of code.

    Version:
    1.00, 28 Dec 1998
    Author:
    Tanmay K. Mohapatra

    Constructor Summary
    MultiPanelLayout(int iMaxX, int iMaxY, java.awt.Container parentContainer)
              Constructor.
     
    Method Summary
     void addLayoutComponent(java.awt.Component comp, java.lang.Object constraints)
              Adds a component to the layout, using a constraint object.
     void addLayoutComponent(java.lang.String name, java.awt.Component comp)
              Adds the specified component with the specified name to the layout.
     float getLayoutAlignmentX(java.awt.Container target)
              Returns the alignment along the x axis.
     float getLayoutAlignmentY(java.awt.Container target)
              Returns the alignment along the y axis.
     void invalidateLayout(java.awt.Container target)
              Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
     void layoutContainer(java.awt.Container parent)
              Lays out the container in the specified panel.
     java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
              Returns the maximum size of this component.
     java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
              Calculates the minimum size dimensions for the specified panel given the components in the specified parent container.
     java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
              Calculates the preferred size dimensions for the specified panel given the components in the specified parent container.
     void removeLayoutComponent(java.awt.Component comp)
              Removes the specified component from the layout.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    MultiPanelLayout

    public MultiPanelLayout(int iMaxX,
                            int iMaxY,
                            java.awt.Container parentContainer)
    Constructor. The layout must be constructed only once and the same instance must be used in all containers which are to be laid out together.
    Parameters:
    iMaxX - the number of grids in the horizontal direction
    iMaxY - the number of grids in the vertical direction
    parentContainer - the parent container containing all components and other containers
    Method Detail

    addLayoutComponent

    public void addLayoutComponent(java.awt.Component comp,
                                   java.lang.Object constraints)
    Adds a component to the layout, using a constraint object. Determines its parent container and adds it to the list of parents.
    Specified by:
    addLayoutComponent in interface java.awt.LayoutManager2

    getLayoutAlignmentX

    public float getLayoutAlignmentX(java.awt.Container target)
    Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.
    Specified by:
    getLayoutAlignmentX in interface java.awt.LayoutManager2

    getLayoutAlignmentY

    public float getLayoutAlignmentY(java.awt.Container target)
    Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.
    Specified by:
    getLayoutAlignmentY in interface java.awt.LayoutManager2

    invalidateLayout

    public void invalidateLayout(java.awt.Container target)
    Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
    Specified by:
    invalidateLayout in interface java.awt.LayoutManager2

    addLayoutComponent

    public void addLayoutComponent(java.lang.String name,
                                   java.awt.Component comp)
    Adds the specified component with the specified name to the layout.

    layoutContainer

    public void layoutContainer(java.awt.Container parent)
    Lays out the container in the specified panel.

    maximumLayoutSize

    public java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
    Returns the maximum size of this component.
    Specified by:
    maximumLayoutSize in interface java.awt.LayoutManager2

    minimumLayoutSize

    public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
    Calculates the minimum size dimensions for the specified panel given the components in the specified parent container.

    preferredLayoutSize

    public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
    Calculates the preferred size dimensions for the specified panel given the components in the specified parent container.

    removeLayoutComponent

    public void removeLayoutComponent(java.awt.Component comp)
    Removes the specified component from the layout.