This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

JLabel centered in a JPanel

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can position a JLabel containing text so that the text is in the middle of the JLabel and the JLabel is right in the middle of the JPanel?

Thanks.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLabel api has constructors that take an argument int horizontalAlignment and also a method setHorizontalAlignment.

The center section of a BorderLayout will center its child component. GridBagLayout will do the same with default GridBagConstraints.
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I would also like the size of the dialog to fit the size the text
in the JLabel. Any suggestion on that?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try calling the pack method on the dialog.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic