Skip to main content

BoxLayout question

1 reply [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Scott Violet

On Fri, Sep 19, 2003 at 02:10:20PM -0400, swing-feedback@javadesktop.org wrote:
> Hi Jacek,
>
> >When adding components into a BoxLayout (a list of
> >JLabels in my example), is there any way to make them
> >fill the full width of the component (sort of like a
> >GridLayout does, but only horizintally, not
> >vertically).
> >
> >I tried setting the miniminSize on all the labels,
> >but it seems to be getting ignored and always gets
> >resized to just the smallest possible one.
>
> You actually would want to set the maximum size. It is the maximum size
> that is preventing the labels from being stretched. Here's a line of
> code:
> [code]
> label.setMaximumSize(new Dimension(Integer.MAX_VALUE,
> label.getMaximumSize().height));
> [/code]

One thing to be careful about with code like this is that if you ever
change a property that effects the height, like the font or border,
you may end up with a max height that is less than the preferred
height, which may confuse some layout managers. If you never
dynamically change these sort of properties, than this code shouldn't
be a problem.

-Scott