JPanel Draw JLabel on left

Hi, I have a panel, with layout: BoxLayout(panel, BoxLayout.Y_AXIS)
It works fine except that the labels and buttons are drawn in the centre whereas I’d like to draw them on the left.
How can I fix this?
Thanks :slight_smile:

Use nested panels.


import java.awt.*;
import javax.swing.*;

public class PanelTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // Equivalent to a JPanel with BoxLayout on y-axis.
                Box box = Box.createVerticalBox();
                box.add(new JLabel("Hello world"));
                box.add(new JButton("Click Me"));
                box.add(new JLabel("I'm another label"));
                box.add(new JButton("Don't click me!!!"));
                JPanel container = new JPanel(new BorderLayout());
                container.add(box, BorderLayout.LINE_START);
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(container);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Thanks BoBear2681 :slight_smile: