On Ali said:
i am not very sure whether the box layout fulfills my second and third
requirements
It depends on what you mean by the second one. For
a vertical orientation I believe it will make all the
components the same width, if they aren't restricted by
maximum width. If it's a column of buttons for example,
they will all be as wide as the largest preferred width.
If the panel is on one side of a split pane or something
and you want the components to fill the width of the
panel but for the panel still to have a meaningful
preferred width, then GridBagLayout would be easy
enough, or you could write your own layout to do this
in about ten minutes. For the simplest case (minimum
size == preferred size, tightly packed components,
align to top, no respect for maximum sizes) you could
do something like (untested, may contain errors):
public Dimension preferredLayoutSize(Container parent) {
Component[] children = parent.getComponents();
Dimension psize = new Dimension();
for(int c = 0; c < children.length; c++) {
Dimension child_psize = children[c].getPreferredSize();
psize.height += child_psize.height;
psize.width = Math.max(psize.width, child_psize.width); }
Insets insets = parent.getInsets();
psize.width += insets.left + insets.right;
psize.height += insets.top + insets.bottom;
return psize;
}
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
public void layoutContainer(Container parent) {
Dimension msize = minimumLayoutSize(parent);
Insets insets = parent.getInsets();
int w = Math.max(parent.getSize().width, msize.width) -
insets.left - insets.right;
int x = insets.left;
int y = insets.top;
Component[] children = parent.getComponents();
for(int c = 0; c < children.length; c++) {
Component child = children[c];
Dimension child_psize = child.getPreferredSize();
child.setLocation(x, y);
child.setSize(w, child_psize.height);
y += child_psize.height;
}
}
public void addLayoutComponent(String name,
Component comp) {
}
public void removeLayoutComponent(Component comp) {
}