wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes BoxLayout alignment issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "BoxLayout alignment issue" Watch "BoxLayout alignment issue" New topic
Author

BoxLayout alignment issue

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hello,
Can someone please explain why my JPanel ( labelPanel - JLables: "One Two Three" ) with a BoxLayout will not align the components to the left instead of the center?



Thank you.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Try experimenting with setAlignmentX(java.awt.Component.LEFT_ALIGNMENT) on your labels.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by James Swan:
Try experimenting with setAlignmentX(java.awt.Component.LEFT_ALIGNMENT) on your labels.


Bingo. You must set them all to have a left alignment. I don't know JLabel sizing properties off the top of my head so it's possible the JLabel will actually expand and set the text in the center. If the above doesn't work try setHorizotnalAlignment(int) to set the text position.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
do you need to use a BoxLayout?

if you change
lablePanel.setLayout( new BoxLayout( lablePanel, BoxLayout.Y_AXIS ) );
to
lablePanel.setLayout( new java.awt.GridLayout( 0,1 ) );

and then this as your buildLabels()
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hello again,
Thanks for the great feedback, however setting the alignment on the JLabel didn't seem to work.

Originally posted by Ken Blair:

Bingo. You must set them all to have a left alignment. I don't know JLabel sizing properties off the top of my head so it's possible the JLabel will actually expand and set the text in the center. If the above doesn't work try setHorizotnalAlignment(int) to set the text position.


Using the GridLayout worked but just a minor problem using this Layout, the JLables are right on the left frame. I know this seems picky, but presentation is important especially when I'm being graded. Gosh, something that seems so simple can be so hard in SWING. Ugh!

Well, I guess worse case scenario, I could use the GridLayout.

Thank again!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the JLables are right on the left frame

labelOne.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,10,0,0));

will nudge it in a bit
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Yee haw, that did it! Thanks Michael...gosh how do you learn this stuff!?!? It's so difficult figuring out what works with what...

Originally posted by Michael Dunn:
> the JLables are right on the left frame

labelOne.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,10,0,0));

will nudge it in a bit


Thanks again!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Shannon Sims:
Hello again,
Thanks for the great feedback, however setting the alignment on the JLabel didn't seem to work.


Did you set the alignment of all of them? You may need to set the alignment of the Box itself too. Alignments do strange things, I never have been able to get an answer on why they come out the way they do. All anyone has ever been able to do is tell me the effect, never the actual root cause. "You have to set them all to 0.0" but never the why the hell Java needs them all to be 0.0, for example.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hi Ken,
Sorry about the delay in response. Yes, I set the alignment on each JLabel and the alignment on the Box was set when I "newed" the Box, without much success. Since using the BoxLayout wasn't mandatory, I used Michael's solution instead.

Thanks for your help, I really appreciate it!
 
 
subject: BoxLayout alignment issue