I have a JPanel (panel), the layout of which is set to BoxLayout. I also have a custom class MapRow, which extends JPanel (and has a few components inside it in a simple FlowLayout), and I wish to add the instances of MapRow to panel in a simple, left-aligned, top-down fashion. Consider the following method:
public void drawMappingsPanel(JPanel panel) {
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
int s = /* aMethodCall() */;
for (int i = 0; i < s; i++) {
MapRow row = new MapRow();
row.setAlignmentX(LEFT_ALIGNMENT);
panel.add(row);
}
}
However, when I run the code, all MapRow panels are centrally aligned, like below:

How can I align the MapRow panels to the left? The setAlignmentX(LEFT_ALIGNMENT) method doesn’t seem to work…
EDIT : I just replaced instances of MapRow with dummy JButtons, and they got left-aligned all fine. So components such as JButtons can be left aligned using setAlignmentX(), but JPanels cannot be?
You should use a LEFT-alignement for you FlowLayout in MapRow. Here is a small SSCCE illustrating that: