alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.form.util;

import java.awt.*;

/**
 * Simple layout managers which arrange components in a row. Components can be
 * top-aligned, vertically center-aligned, bottom-aligned or stretched
 * vertically to fill the available space
 * @author Tran Duc Trung
 */

public class RowLayout implements LayoutManager
{
    public static final int TOP = 1;
    public static final int BOTTOM = 2;
    public static final int CENTER = 3;
    public static final int FILL = 4;

    private int	_align;
    private int	_gap;

    public RowLayout() {
        this(0);
    }

    public RowLayout(int gap) {
        this(gap, FILL);
    }

    public RowLayout(int gap, int align) {
        _gap = gap;
        _align = align;
    }

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

    public void removeLayoutComponent(Component comp) {}

    public Dimension preferredLayoutSize(Container parent) {
        Dimension size = new Dimension();

        synchronized(parent.getTreeLock()) {
            Component[] comps = parent.getComponents();
            for (int i = 0; i < comps.length; i++) {
                Dimension d;
                d = comps[i].getPreferredSize();
                size.width += d.width;
                size.height = Math.max(size.height, d.height);
                if (i < comps.length - 1)
                    size.width += _gap;
            }

            Insets insets = parent.getInsets();
            size.width += insets.left + insets.right;
            size.height += insets.top + insets.bottom;
        }
        return size;
    }

    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }

    public void layoutContainer(Container parent) {
        synchronized(parent.getTreeLock()) {
            Component[] comps = parent.getComponents();
            Insets insets = parent.getInsets();
            Dimension size = parent.getSize();

            int x = insets.left;
            int height = size.height - insets.top - insets.bottom;

            for (int i = 0; i < comps.length; i++) {
                Component c = comps[i];
                Dimension d = c.getPreferredSize();

                int ofs = 0;
                int h = d.height;

                switch (_align) {
                    case TOP:
                        ofs = 0;
                        break;
                    case BOTTOM:
                        ofs = height - d.height;
                        break;

                    case CENTER:
                        ofs =(height - d.height) / 2;
                        break;

                    case FILL:
                        ofs = 0;
                        h = height;
                        break;
                }

                c.setBounds(x, insets.top + ofs, d.width, h);
                x += d.width + _gap;
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.