views:

37

answers:

2

I used myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS)); to order panels vertically (one under another one). But myPanel defined in the shown way change the included panel. In more details, it tries to set the same hight and width for the included panels. Can it be changed? Can BoxLayout adopt size of the included elements?

ADDED:

So, as it is recommended, let's read the documentation:

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

In my case, I have the Y_AXIS layout. Is it the "vertical" layout? If it is the case, the components should be arranged at their preferred heights. What is the "preferred heights"?

Next, it is written "for a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component". So, I assume that for the vertical layout. BoxLayout will try to make all components as broad as the broadest one. Right? And what about the "preferred heights" used in the vertical layout?

It is also written that if it is not possible for the horizontal layout to make all components as hight as the highest component it will align the component vertically. What does it mean "to align vertically"? Is it not strange that the horizontal layout aligns component vertically?

ADDED 2:

The problem of the vertical layout is that it makes all elements as broad as the window. As a result, elements included into the JPanel, with the vertical box layout, will not keep their proportions. And this is exactly what I want to avoid.

A: 

From the Javadoc:

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

Adamski
+1  A: 

Use GridBagLayout instead. It is a pain to configure manually, but there are tools that can write the constraints for you (most notably NetBeans' GUI builder) -- and it gives you total control over how each of the components are stretched and located.

You can set alignments, fills, insets, margins, how the free space gets distributed, how the final bounds will be calculated, and so on. And it is not so difficult to write looping code to add whole collections of components with similar constraints in either rows or columns.

tucuxi