Please suggest a layout manager

O

On Ali

Hi,

Can anyone suggest a layout manager which would do the following:

- Stack the components one below the other (Not the way CardLayout
does, but like a grid layout with 1 column)
- Set the width of each component as that of the panel (minus insets)
- Set the height as the preferred height

The behavior I want is similar to the windows explorer details panel
on the left. (Some blocks can be opened or closed using arrow buttons)

Thanks in advance,

Regards,
On Ali
 
E

Eric Sosman

Hi,

Can anyone suggest a layout manager which would do the following:

- Stack the components one below the other (Not the way CardLayout
does, but like a grid layout with 1 column)
- Set the width of each component as that of the panel (minus insets)
- Set the height as the preferred height

Perhaps you want BoxLayout with a vertical orientation?
> The behavior I want is similar to the windows explorer details panel
> on the left. (Some blocks can be opened or closed using arrow buttons)

When you change the size of an already-painted component,
you'll probably need to re-pack() the container to inform the
layout manager of the change.
 
O

On Ali

Perhaps you want BoxLayout with a vertical orientation?


When you change the size of an already-painted component,
you'll probably need to re-pack() the container to inform the
layout manager of the change.

i am not very sure whether the box layout fulfills my second and third
requirements
 
E

Eric Sosman

i am not very sure whether the box layout fulfills my second and third
requirements

It does -- or so it seems to me; I may have misunderstood
your requirements. Why not try it for yourself?
 
O

On Ali

It does -- or so it seems to me; I may have misunderstood
your requirements. Why not try it for yourself?

i have actually seen the screenshots on the sun site.... i takes the
preferred width of the component and not the total available width of
the container
 
L

Larry Barowski

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) {
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top