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
posted
0
Try experimenting with setAlignmentX(java.awt.Component.LEFT_ALIGNMENT) on your labels.
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
0
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.
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: 187
posted
0
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.
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: 187
posted
0
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!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.