Making BoxLayout move components to top while stacking left to right
Riduidel is correct about setting setAlignmentY
on the GameSelectionPanel
itself, and GridBagLayout
is an excellent alternative. If you prefer to stick with BoxLayout
, the article Fixing Alignment Problems discusses the matter, suggesting "all the components controlled by a left-to-right Boxlayout should generally have the same Y alignment." In your example, add
botSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
blindSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
Well, the setAlignmentY
method has no effect here, since it acts on the panel considered as a component.
As you have guessed, the layout of contained panels is defined by the layout manager you use.
Unfortunatly, BoxLayout
do not provide the kind of feature you're looking at.
in standard JDK, obviously, the layout of choice for your problem is GridBagLayout
. Although rather hard to understand at first, it will fast reveal to you its power in components arrangement.
using the useful GBC class, your components could be arranged as such :
setLayout(new GridBagLayout(this));
add(botSelectionPanel, new GBC(0,1).setAnchor(TOP));
add(blindSelectionPanel, new GBC(0,2).setAnchor(TOP));
or I think so ;-)