Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to center buttons with fixed sizes  RSS feed

 
Karen Nelson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you! From what I read, GridBagLayout seemed to complicated to use at all by hand. Works like a charm.
-Karen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!