views:

257

answers:

2

Hello,

I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:

  1. I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)

  2. I want the buttons to have a little bit of space between them (let's say 10 px)

Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?

+2  A: 

A couple of suggestions

  1. Try setting the preferredSize to a suitable Dimension value
  2. If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
  3. If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
Don
Thanks, setting the minimum/maximum size of the buttons worked. I'm now looking into the GridBagLayout.
FinalArt2005
Setting the preferredSize only seems to work for the height, not the width.
FinalArt2005
+2  A: 

If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.

camickr
Thanks, I've looked into the GridBagLayout but I think it is a bit too elaborate for the couple of buttons that I have, so I'll try this GridLayout now (already came across it when reading about the GridBagLayout). If I can set a gap then that would be great.
FinalArt2005