I don’t understand how BoxLayout
lays out its components
My goal is simple: to left-align a label (and keep it in the center vertically). Here’s what I discovered:
setAlignmentX(1)
seemingly does the trick. Though, I’m curious why it’s notsetAlignmentX(0)
: after all theComponent.LEFT_ALIGNMENT
constant is set to zerosetHorizontalAlignment()
makes no difference- If I expand the frame horizontally, the extra space is distributed in an unexpected way. Most of it goes to the left of my label, some to the right. I expected all of it go to the right (after all, it’s left-aligned)
The questions are:
- Why did
BoxLayout
position my label that way? - Does it mean I have to wrap the label in some container to make it work? That’s a lot of “hoop jumping” for such a trivial objective (see the “fixer” snippets)
An MRE:
import di.Frames;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.stream.Stream;
public class BoxLayoutDemo {
public static void main(String[] args) {
Frames.frame("Box Layout", createMainPanel()).setVisible(true);
}
private static JPanel createMainPanel() {
JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout);
panel.setBackground(Color.LIGHT_GRAY);
panel.add(createLabel());
panel.add(createCheckBoxPanel());
return panel;
}
private static JLabel createLabel() {
JLabel label = new JLabel("Check boxes");
label.setAlignmentX(1);
//label.setHorizontalAlignment(SwingConstants.LEFT); // ignored anyway
label.setOpaque(true);
label.setBackground(Color.CYAN);
return label;
}
private static JPanel createCheckBoxPanel() {
JPanel panel = new JPanel();
GridLayout layout = new GridLayout(1, 0);
panel.setLayout(layout);
Stream.generate(JCheckBox::new).limit(5).forEach(panel::add);
return panel;
}
}
package di;
import javax.swing.*;
import java.awt.*;
public class Frames {
private Frames() {
}
public static JFrame frame(Container mainPanel) {
return frame(null, mainPanel);
}
public static JFrame frame(String title, Container mainPanel) {
JFrame frame = new JFrame(title);
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
return frame;
}
}
before resizing
after resizing
A “fixer”. It fixes the horizontal alignment, but the vertical alignment is problematic (I’d like it to vertically stay in the center). If you expand the frame vertically, the label sticks to the top of its container
private static Component createLabel() {
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
panel.setLayout(layout);
JLabel label = new JLabel("Check boxes");
label.setOpaque(true);
label.setBackground(Color.CYAN);
panel.add(label);
panel.setBackground(Color.GRAY);
return panel;
}
aligned alright
A better “fixer”. Now, the panel is wrapped in a BoxLayout
-managed panel. Both horizontal and vertical alignments now meet my expectations. However, I’m not comfortable with the amount of code I have to write for such a simple thing
private static Component createLabel() {
JPanel panel = new JPanel();
BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
panel.setLayout(layout);
JLabel label = new JLabel("Check boxes");
label.setOpaque(true);
label.setBackground(Color.CYAN);
panel.add(label);
panel.add(Box.createGlue());
panel.setBackground(Color.GRAY);
return panel;
}
aligned alright, including vertically
Note 1. I simply need a stack of spaced components (components with gaps between them). GridLayout(0, 1)
makes all the rows equal in height which is not what I want. That’s why I chose BoxLayout
. Glue or rigid areas can be manually added for gaps
Note 2. I did read this and this questions. However, I didn’t find the answers I am looking for
3
label.setAlignmentX(1);
Don’t use magic numbers. People reading the code don’t know what “1” represents. Swing provides a constant variable that can be used:
label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
BoxLayout does weird things with the alignment of components. Read the section from the swing tutorial on Fixing Alignment Problems.
In short, make sure the alignment of the components added to the panel with the BoxLayout have the same alignment.
The default for a JPanel is center aligned. So, in this case, you need to make sure the JPanel is also left aligned:
panel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
2