jApp
Class BetterBoxLayout

java.lang.Object
  extended by jApp.BetterBoxLayout
All Implemented Interfaces:
java.awt.LayoutManager

public class BetterBoxLayout
extends java.lang.Object
implements java.awt.LayoutManager

Implements a layout quite similar to javax.swing.BoxLayout, only better.

BetterBoxLay out can be used to lay out components in row or a column. In the following description horizontal (row) orientation is assumend. To understand how the vertical orientation works, just re-read the description and replace/swap column for row, width for height, horizontal for vertical etc. The following constants are used to set the orientation of the layout. These and other constants need to be combined and passed to the constructor to define the layout behaviour.

  • X_AXIS
  • Y_AXIS
  • In the horizontal direction components are laid out in a row, one after another. Glue objects, struts and rigid areas can be inserted, just like with a regular BoxLayout.

    Here is a short description of glue, struts and rigid areas, all of which are invisible objects:

  • a glue objects is a zero sized component that will stretch if space is available.
  • a strut is like a one dimensional spacer, it will occupy space but in one direction only
  • a rigid area is a two dimensional spacer, and like a strut it will not stretch or shrink
  • Horizontal Component Placement / Width

    The components minimum and maximums sizes are always observed.

    If there are no glue objects then the components are resized in the horizontal direction as needed to fill the space available, (within the components minimum and maximum widths). When resising the components the relative widths of components (as defined by their preferred widths) are maintained as far as possible.

    If there are glue objects and there is extra in the horizontal direction then the extra space is used to expand those glue objects evenly to fill the available space.

    If there are no glue objects and the components cannot be stretched to fill the space available, then the extra space is added to the beginning and enf of the row.

    Component Height

    The components minimum and maximums sizes are always observed.

    Each components height is determined by its minimum, maximum, preferred or container's height depending on the passed to the constructor of layout manager:

  • MINIMUM_HEIGHT - each component will obey its minimum height
  • PREFERRED_HEIGHT- each component will obey its preferred height
  • MAXIMUM_HEIGHT - each component will obey its maximum height
  • CONTAINER_HEIGHT - each component will obey the containers height
  • In addtion to those the following flag can be used to ensure that no component will be higher than it's container.

  • CLIPPED_HEIGHT
  • Vertical Component Placement

    In the vertical direction all components on the same line. If there is extra space in the vertical direction then the components Y-aligment value is used to determine where (vertically) in the available space the the component is placed. An allignment value of 0.0F will align the top of the component with the top of the container and a value of 1.0F will allign the bottom with bottom (if possible).

    A major difference from javax.swing.BoxLayout is that BetterBoxLayout does NOT ignore invisible components (by default). If hidden components are NOT ignored then hiding a component does NOT affect the layout. This is usually better behaviour because otherwise the remaining (visible) buttons would move, which is (usually) poor UI practice. Disabling buttons is of course often an option but there are many situation in which the preferred choise is hiding.

    The following constant can be used tell the layout manager to ignore hidden (invisible) components.

  • IGNORE_HIDDEN
  • Copyright: Copyright (c) 2004 Kusta Nyholm Oy

    Version:
    1.0
    Author:
    Kustaa Nyholm

    Field Summary
    static int CLIPPED_HEIGHT
              Limit component height to container height
    static int CLIPPED_WIDTH
              Limit component width to container width
    static int CONTAINER_HEIGHT
              Use container height for component height
    static int CONTAINER_WIDTH
              Use container widht for component height
    static int IGNORE_HIDDEN
              Instruct layout manager to ignore hidden components
    static int MAXIMUM_HEIGHT
              Maximise component height
    static int MAXIMUM_WIDTH
              Maximise component width
    static int MINIMUM_HEIGHT
              Mimimize component height
    static int MINIMUM_WIDTH
              Mimimize component width
    static int PREFERRED_HEIGHT
              Use component preferred height
    static int PREFERRED_WIDTH
              Use component preferred width
    static int X_AXIS
              Horizontal orientation
    static int Y_AXIS
              Vertical orientation
     
    Constructor Summary
    BetterBoxLayout()
              Creates a horizontal layout where components are their respective preferred sizes clipped to the container height.
    BetterBoxLayout(int flags)
              Creates a layout based on the flags passed.
     
    Method Summary
     void addLayoutComponent(java.lang.String name, java.awt.Component comp)
              Not used by this layout manager.
    static javax.swing.JComponent createGlue()
              Creates a glue object.
    static javax.swing.JPanel createHorizontalBox()
               
    static javax.swing.JComponent createHorizontalGlue()
              Creates a horizontal glue object.
    static javax.swing.JComponent createHorizontalStrut(int w)
              Creates a horizontal strut object of a given width.
    static javax.swing.JComponent createRigidArea(int w, int h)
              Createas an invisible (transparent), space occupying, non resizable component of given width and height.
    static javax.swing.JPanel createVerticalBox()
               
    static javax.swing.JComponent createVerticalGlue()
              Creates a vertical glue object.
    static javax.swing.JComponent createVerticalStrut(int h)
              Creates a vertical strut object of a given height.
     double getAlignmentY(java.awt.Component c)
               
     void layoutContainer(java.awt.Container container)
              Called by the AWT when the specified container needs to be laid out.
     java.awt.Dimension maximumSize(java.awt.Container container)
               
     java.awt.Dimension minimumLayoutSize(java.awt.Container container)
               
     java.awt.Dimension preferredLayoutSize(java.awt.Container container)
               
     void removeLayoutComponent(java.awt.Component comp)
              Not used by this layout manager.
    static void test()
              Opens a few test windows to test/debug the layout manager.
    static void test1()
               
    static void test2()
               
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    X_AXIS

    public static final int X_AXIS
    Horizontal orientation

    See Also:
    Constant Field Values

    Y_AXIS

    public static final int Y_AXIS
    Vertical orientation

    See Also:
    Constant Field Values

    MINIMUM_HEIGHT

    public static final int MINIMUM_HEIGHT
    Mimimize component height

    See Also:
    Constant Field Values

    MINIMUM_WIDTH

    public static final int MINIMUM_WIDTH
    Mimimize component width

    See Also:
    Constant Field Values

    PREFERRED_HEIGHT

    public static final int PREFERRED_HEIGHT
    Use component preferred height

    See Also:
    Constant Field Values

    PREFERRED_WIDTH

    public static final int PREFERRED_WIDTH
    Use component preferred width

    See Also:
    Constant Field Values

    MAXIMUM_HEIGHT

    public static final int MAXIMUM_HEIGHT
    Maximise component height

    See Also:
    Constant Field Values

    MAXIMUM_WIDTH

    public static final int MAXIMUM_WIDTH
    Maximise component width

    See Also:
    Constant Field Values

    CONTAINER_HEIGHT

    public static final int CONTAINER_HEIGHT
    Use container height for component height

    See Also:
    Constant Field Values

    CONTAINER_WIDTH

    public static final int CONTAINER_WIDTH
    Use container widht for component height

    See Also:
    Constant Field Values

    CLIPPED_HEIGHT

    public static final int CLIPPED_HEIGHT
    Limit component height to container height

    See Also:
    Constant Field Values

    CLIPPED_WIDTH

    public static final int CLIPPED_WIDTH
    Limit component width to container width

    See Also:
    Constant Field Values

    IGNORE_HIDDEN

    public static final int IGNORE_HIDDEN
    Instruct layout manager to ignore hidden components

    See Also:
    Constant Field Values
    Constructor Detail

    BetterBoxLayout

    public BetterBoxLayout()
    Creates a horizontal layout where components are their respective preferred sizes clipped to the container height.


    BetterBoxLayout

    public BetterBoxLayout(int flags)
    Creates a layout based on the flags passed.

    Parameters:
    flags - see class description for usage
    if no axis flad is specified X_AXIS is assumed
    if no height flag is specified PREFERRED_HEIGHT is assumed
    Throws:
    java.lang.IllegalArgumentException - if conflicting flags are detected
    Method Detail

    addLayoutComponent

    public void addLayoutComponent(java.lang.String name,
                                   java.awt.Component comp)
    Not used by this layout manager.

    Specified by:
    addLayoutComponent in interface java.awt.LayoutManager
    Parameters:
    name -
    comp -

    removeLayoutComponent

    public void removeLayoutComponent(java.awt.Component comp)
    Not used by this layout manager.

    Specified by:
    removeLayoutComponent in interface java.awt.LayoutManager
    Parameters:
    comp -

    preferredLayoutSize

    public java.awt.Dimension preferredLayoutSize(java.awt.Container container)
    Specified by:
    preferredLayoutSize in interface java.awt.LayoutManager

    minimumLayoutSize

    public java.awt.Dimension minimumLayoutSize(java.awt.Container container)
    Specified by:
    minimumLayoutSize in interface java.awt.LayoutManager

    maximumSize

    public java.awt.Dimension maximumSize(java.awt.Container container)

    getAlignmentY

    public double getAlignmentY(java.awt.Component c)

    layoutContainer

    public void layoutContainer(java.awt.Container container)
    Called by the AWT when the specified container needs to be laid out.

    Specified by:
    layoutContainer in interface java.awt.LayoutManager
    Parameters:
    container - the container to lay out
    Throws:
    java.awt.AWTError - if a sharing situation is detected between the specified container and one passed to this routine before.

    createGlue

    public static javax.swing.JComponent createGlue()
    Creates a glue object. Glue objects are realy not directional, so this is all you need.

    Returns:
    a new instance of a glue object

    createHorizontalGlue

    public static javax.swing.JComponent createHorizontalGlue()
    Creates a horizontal glue object. This is provided just for mnemoni and symmetry reasons. Glue objects are real not directional, so you could use createGlue instead.

    Returns:
    a new instance of a glue object

    createVerticalGlue

    public static javax.swing.JComponent createVerticalGlue()
    Creates a vertical glue object. This is provided just for mnemoni and symmetry reasons. Glue objects are real not directional, so you could use createGlue instead.

    Returns:
    a new instance of a glue object

    createHorizontalStrut

    public static javax.swing.JComponent createHorizontalStrut(int w)
    Creates a horizontal strut object of a given width. A horizontal strut occupies space in horizontal direction only.

    Parameters:
    w - the width of the strut
    Returns:
    a new instance of a strut object with given width

    createVerticalStrut

    public static javax.swing.JComponent createVerticalStrut(int h)
    Creates a vertical strut object of a given height. A vertical strut occupies space in vertical direction only.

    Parameters:
    h - the height of the strut
    Returns:
    a new instance of a strut object with given width

    createRigidArea

    public static javax.swing.JComponent createRigidArea(int w,
                                                         int h)
    Createas an invisible (transparent), space occupying, non resizable component of given width and height.

    Parameters:
    w - the width of the rigid area
    h - the height of the rigid area
    Returns:
    a new instance of a rigid area object with given width and height

    createHorizontalBox

    public static javax.swing.JPanel createHorizontalBox()

    createVerticalBox

    public static javax.swing.JPanel createVerticalBox()

    test

    public static void test()
    Opens a few test windows to test/debug the layout manager.


    test1

    public static void test1()

    test2

    public static void test2()