• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

BoxLayout alignment issue

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try experimenting with setAlignmentX(java.awt.Component.LEFT_ALIGNMENT) on your labels.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic