Class OptimalBoxLayout

  extended by java.awt.GridLayout
      extended by
All Implemented Interfaces:

public class OptimalBoxLayout
extends java.awt.GridLayout

An optimal box layout can lay out equal-size components in a grid so that the preferred component proportion is best preserved. This layout is an extension of the grid layout, but the user does not specify the number of rows and columns, as the layout determines the optimal number of rows and columns automatically.

At construction the preferred component proportion is specified as a width/height value. The layout gets the number of components from the container that uses the layout. With that information a certain number of possible grid configurations is made. The possible grid configurations are n rows x Math.ceil(nboxes/n) columns, with n varying from 1 to nboxes (the number of components). There may be empty grid cells, because n*Math.ceil(nboxes/n) can be greater than nboxes. To make optimal use of the available space, the number of empty grid cells is minimised, so only those configurations with a minimal number of empty cells are considered.

For each of the remaining configurations it is calculated how large each component can be if the preferred proportion is required to be preserved and as much of the available container space is used. This results into a component scale factor, which is maximised. There will be 1 or 2 configurations with a maximal scale factor.

If there are 2 configurations, it means that in one of them the components are scaled so that they cover the entire container width, and in the other the components are scaled so that they cover the entire container height. In the end however, because of the grid layout, the entire container space will be filled, so in the former configuration the components will be stretched vertically (to cover the remaining container height), while in the latter configuration the component will be stretched horizontally (to cover the remaining container width). At construction of the layout it is specified which of the two is preferred.

When an optimal configuration is found, the result is an optimal number of rows and columns, which is used to lay out the components in a grid using the grid layout from which this layout extends.

See Also:
Serialized Form

Constructor Summary
OptimalBoxLayout(double boxProportion, boolean preferWidthStretch)
          Constructs a new optimal box layout.
Method Summary
 void layoutContainer(java.awt.Container parent)
 java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
 java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
Methods inherited from class java.awt.GridLayout
addLayoutComponent, getColumns, getHgap, getRows, getVgap, removeLayoutComponent, setColumns, setHgap, setRows, setVgap, toString
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail


public OptimalBoxLayout(double boxProportion,
                        boolean preferWidthStretch)

Constructs a new optimal box layout. The parameters specify the preferred component proportion as a double value of a component's width divided by its height.

The layout may find two optimal grid configurations. In one of them the components will be stretched horizontally so they will be wider than the preferred proportion. In the other configuration the components will be stretched vertically so they will be higher than preferred. The preferWidthStretch parameter determines which of the two configurations will be chosen.

boxProportion - the preferred component proportion (width/height)
preferWidthStretch - true if it is preferred that components are stretched horizontally rather than vertically, false if it is preferred that components are stretched vertically rather than horizontally
Method Detail


public void layoutContainer(java.awt.Container parent)
Specified by:
layoutContainer in interface java.awt.LayoutManager
layoutContainer in class java.awt.GridLayout


public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
minimumLayoutSize in class java.awt.GridLayout


public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
preferredLayoutSize in class java.awt.GridLayout