File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JLabel centered in a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel centered in a JPanel" Watch "JLabel centered in a JPanel" New topic
Author

JLabel centered in a JPanel

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Jun 05, 2001
Posts: 303
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

Joined: Jan 14, 2004
Posts: 1535
I would try calling the pack method on the dialog.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JLabel centered in a JPanel