Building your own manager

The following manager sets the size of its (single) child (ignoring insets):

class SizeLayout implements LayoutManager {
    Dimension size;

    public SizeLayout() {
        size = new Dimension(0, 0);
    }

    public SizeLayout(Dimension s) {
        size = s;
    }

    public void setSize(Dimension s) {
        size = s;
    }

    public Dimension getSize() {
        return size;
    }

    public void addLayoutComponent(String n, Component c) {
    }

    public void removeLayoutComponent(Component c) {
    }

    public Dimension preferredLayoutSize(Container parent) {
        if (parent.countComponents() == 0)
            return new Dimension(width, height);

        // use the first component added
        Component c = parent.getComponent(0);
        Dimension d = new Dimension(c.preferredSize());
        return d;
    }

    public Dimension minimumLayoutSize(Container parent) {
        if (parent.countComponents() == 0)
            return new Dimension(width, height);

        // use the first component added
        Component c = parent.getComponent(0);
        Dimension d = new Dimension(c.minimumSize());
        return d;
    }

    public void layoutContainer(Container parent) {
        if (parent.countComponents() == 0)
            return;

        // use the first component added
        Component c = parent.getComponent(0);
        c.reshape(0, 0, size.width, size.height);
        c.validate();
    }
}


Slide 95 ©Copyright 1997 Jan Newmarch