Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Alignment in BoxLayout

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have had this problem with a few apps, but just worked my around them. But I kind of need a solution to the problem now. I have a JDialog and I am using a BoxLayout with Y Alignment. I am adding JLabel, JTextField, JLabel, JTextField....
I need the JLabels to align to the left so I am using setAlignmentX(Component.LEFT_ALIGNMENT) but it is not moving the labels. Can someone look at my code and see if they can see what I am not seeing?
Thanks.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your example, the best solution is to replace the BoxLayout with a new GridLayout( 0, 1 ). I think BoxLayout centers it's components, and I don't see a way to change this without extending the class. In this case the fix is easy, just use a 1 column GridLayout, otherwise you'd have to use GridBagLayout or something that lets you mess with the placement of individual components more.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will probably go ahead and do that. However, the SWING tutorial says this should be possible.
Fixing Alignment Problems
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic