The problem is that JLabel aligns the right edge with the center of the panel, and scrolling aligns its center with the center of the panel.
I was able to fix this by adding two lines, setting the horizontal alignment of both label and jscrlpBox to Component.LEFT_ALIGNMENT :
JLabel label = new JLabel("Option"); label.setAlignmentX(Component.LEFT_ALIGNMENT); // Added panel.add(label); JLabel a = new JLabel("A"); JLabel b = new JLabel("B"); JLabel c = new JLabel("C"); JLabel d = new JLabel("D"); JLabel e = new JLabel("E"); Box box = Box.createVerticalBox(); box.add(a); box.add(b); box.add(c); box.add(d); box.add(e); JScrollPane jscrlpBox = new JScrollPane(box); jscrlpBox.setPreferredSize(new Dimension(140, 90)); panel.add(jscrlpBox); jscrlpBox.setAlignmentX(Component.LEFT_ALIGNMENT); // Added
In general, when troubleshooting issues like this, I try to add a brightly colored line border to components (in this case label ) that are not where I want them. This is when I realized that he aligned the right edge with the same line as the other component to align its center.
source share