• 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
Pie
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try calling the pack method on the dialog.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic