This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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.
 
 
subject: JLabel centered in a JPanel
 
Similar Threads
Transparent color for the panel !
ActonListener
Java Group Boxes
change font in JPanel
Weirdness when changing CENTER of a JPanel with BorderLayout