954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JLable Alignement in Box

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?

Kob0724
Junior Poster in Training
52 posts since Jul 2005
Reputation Points: 30
Solved Threads: 0
 

Which layout manager are you using?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

pane.add(baconPath, "East");


should I be using a different Layout manager?

Kob0724
Junior Poster in Training
52 posts since Jul 2005
Reputation Points: 30
Solved Threads: 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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

Kob0724
Junior Poster in Training
52 posts since Jul 2005
Reputation Points: 30
Solved Threads: 0
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You