Two Columned Layout for standart forms

I have implemented a layout for panels with two columns. It calculates the width of the first column according to the widest component of first column. And, uses remained space for second column components. Besides, each row height is calculated according to each row's two inner components' heights. It may be useful for some cases.

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;

public class TwoColumnedLayout implements LayoutManager {
    private int horizontalGap = 0;
    private int verticalGap = 0;

    public TwoColumnedLayout() {
    }

    public TwoColumnedLayout(int horizontalGap, int verticalGap) {
        this.horizontalGap = horizontalGap;
        this.verticalGap = verticalGap;
    }

    @Override
    public Dimension preferredLayoutSize(Container parent) {
        int count = parent.getComponentCount();
        if (count == 0) {
            return minimumLayoutSize(parent);
        }
        int totalHeight = 0;
        int oddComponentHeight = 0;
        int maxOddWidth = 1;
        int maxEvenWidth = 1;
        for (int i = 0; i < count; ++i) {
            Component subComponent = parent.getComponent(i);
            Dimension size = subComponent.getPreferredSize();
            if (i % 2 == 0) {
                maxOddWidth = Math.max(maxOddWidth, size.width);
                oddComponentHeight = size.height;
                if (i > 0) {
                    totalHeight += verticalGap;
                }
            }
            else {
                maxEvenWidth = Math.max(maxEvenWidth, size.width);
                int maxHeight = Math.max(oddComponentHeight, size.height);
                totalHeight += maxHeight;
                oddComponentHeight = 0;
            }
        }
        totalHeight += oddComponentHeight;
        return new Dimension(maxOddWidth + horizontalGap + maxEvenWidth, totalHeight);
    }

    @Override
    public void layoutContainer(Container parent) {
        int count = parent.getComponentCount();
        if (count == 0) {
            return;
        }
        Dimension parentSize = parent.getSize();
        int parentWidth = parentSize.width;
        int oddWidth = 1;
        for (int i = 0; i < count; ++i) {
            Component subComponent = parent.getComponent(i);
            Dimension componentSize = subComponent.getPreferredSize();
            if (i % 2 == 0) {
                oddWidth = Math.max(oddWidth, componentSize.width);
            }
        }
        int evenWidth = parentWidth - oddWidth - horizontalGap;
        if (evenWidth < 0) {
            evenWidth = 1;
        }
        int y = 0;
        int oddHeight = 0;
        for (int i = 0; i < count; ++i) {
            Component subComponent = parent.getComponent(i);
            Dimension componentSize = subComponent.getPreferredSize();
            int componentHeight = componentSize.height;
            if (i % 2 == 0) {
                subComponent.setSize(oddWidth, componentHeight);
                subComponent.setLocation(0, y);
            }
            else {
                subComponent.setSize(evenWidth, componentHeight);
                subComponent.setLocation(oddWidth + horizontalGap, y);
                int rowHeight = Math.max(oddHeight, componentHeight);
                y += rowHeight + verticalGap;
            }
        }
    }

    @Override
    public Dimension minimumLayoutSize(Container parent) {
        return new Dimension(1, 1);
    }

    @Override
    public void addLayoutComponent(String name, Component comp) {
    }

    @Override
    public void removeLayoutComponent(Component comp) {
    }
}

0 comment(s):