This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes trying to center buttons with fixed sizes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "trying to center buttons with fixed sizes" Watch "trying to center buttons with fixed sizes" New topic
Author

trying to center buttons with fixed sizes

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Hi,

Is there any easy way to vertically center buttons in a panel? I am trying to display a column of buttons at a size of 60x60 pixels for each button. The code below does everything except the centering fine -- but of course if I add the buttons to the CENTER of the panel, they get stretched.

I guess I could use an empty border around the panel, and calculate its size by subtracting the size of the buttons from the height of the window etc, but is there an easier way?

Thank you!

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
change this
JPanel subPanel = new JPanel(new BorderLayout());
subPanel.add(buttonPanel, BorderLayout.SOUTH);

to this
JPanel subPanel = new JPanel(new GridBagLayout());
subPanel.add(buttonPanel, new GridBagConstraints());

unless you intend adding something else to 'subPanel'
(it being a borderlayout, and specifying south)
Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Wow, thank you! From what I read, GridBagLayout seemed to complicated to use at all by hand. Works like a charm.
-Karen
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: trying to center buttons with fixed sizes
 
Similar Threads
Is this really where actionlistener should go?
double to text
JLabel appears as a JButton
Problem refreshing a panel
Frames