I'm adding a few JLabels to a Box like this:

actor1=new JLabel("");
    actor2=new JLabel("");
    actor3=new JLabel("");
    actor4=new JLabel("");
    actor5=new JLabel("");
    actor6=new JLabel("");
    actor7=new JLabel("");
    movie1=new JLabel("");
    movie2=new JLabel("");
    movie3=new JLabel("");
    movie4=new JLabel("");
    movie5=new JLabel("");
    movie6=new JLabel("");
    
    baconPath = Box.createVerticalBox();
    baconPath.add(actor1);
    baconPath.add(movie1);
    baconPath.add(actor2);
    baconPath.add(movie2);
    baconPath.add(actor3);
    baconPath.add(movie3);
    baconPath.add(actor4);
    baconPath.add(movie4);
    baconPath.add(actor5);
    baconPath.add(movie5);
    baconPath.add(actor6);
    baconPath.add(movie6);
    baconPath.add(actor7);

When the box displays all the JLabels are aligned to the Right. How can I get them to align to the left?

Recommended Answers

All 5 Replies

Which layout manager are you using?

I'm using the border layout. Later on I use this

pane.add(baconPath, "East");

should I be using a different Layout manager?

uh no. You're using the default LayoutManager of the Box you're creating, which is not BorderLayout.
If it were, you couldn't add those labels to it like that ;)
And the default (and indeed only allowerd) LayoutManager for a Box is BoxLayout.

To change the alignment of your Components inside the Box, you'll need to add those Components each to a JPanel, which you give a FlowLayout, which you set to the alignment you want.

Bummer, I was hoping I wouldn't have to do that. But you're right. It did work. Thanks a bunch!

the system of layout managers is highly flexible and very powerful but with that comes complexity and a learning curve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.