0

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?

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by jwenting
0

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

pane.add(baconPath, "East");

should I be using a different Layout manager?

0

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.

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.