tags:

views:

89

answers:

3
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
+1  A: 

The BoxLayout is easy - the argument (Y_AXIS) to the BoxLayout constructor belongs to the BoxLayout class so you need to specify that. There may well be another Y_AXIS value which would be the wrong one to use.

This is the basis of encapsulation.

With regards to the panel double-mention, I can't be sure. But it seems logical that the panel needs to be told what its layout manager is and the layout manager needs to know which panel it's applied to (since it needs to know the contents of the panel in order to lay them out intelligently.

paxdiablo
+1  A: 

The panel arguement to BoxLayout is somewhat confusing. You need to specify the target that the BoxLayout instance is attached to. Further, in many methods based on the LayoutManager2 Interface, BoxLayout requires the panel to be passed in and it performs a check that you have passed in the right one.

The Y_AXIS is a class variable that is one of two legal int values for the orientation of the BoxLayout. You could just pass in the value 0, but using the constant is much better.

akf
A: 

Well mostly because BoxLayout is an old API before they knew any better.

Use MigLayout if you are interested in a good flexible modern layout manager

Gregory Mostizky