my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Trouble Aligning JButtons (And setting width) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic

Trouble Aligning JButtons (And setting width)

Andrew Ceronoski

Joined: Apr 21, 2009
Posts: 16

Im trying to make 4 JButtons the same width, have space in between each other, to be aligned vertically in a JPanel.

I can add all four of the JButtons no problem, and even have them vertically aligned. But I cannot get them to have the same widths.

Any ideas?

Thank you!
K. Tsang

Joined: Sep 13, 2007
Posts: 3131

I don't think the API has a setWidth or similar function for JButtons. The width of the buttons is determined by the text inside the button. So the "ABC" will be shorter than "ABCDEF". If you are keen on fixing the width you probably need to do a bit of string manipulation setting each text for the JButtons the same before initializing the JButtons.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
experiment with the setPreferredSize method, which can be applied to JButton. Also look at layout managers which force components to be the same size. I think GridLayout is one of them.

I found the following page to be very helpful. It's a Sun document, but it's not a part of the Java Tutorial
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

GridLayout sounds like a good idea. If you can't make that work you could start with this class. It overrides the getPreferredSize method by referring to another button's preferred size. You might want to modify it by adding constructors or allowing it to look at more other buttons.
Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Sorry if I am bringing an old thread back to life inappropriately, but I am currently have the same problem.

I have a swing application using GridBagLayout. It doesn't look pretty, but it gets the job done. However, I can't get anything centered or looking right no matter what I do.

Here is the code for the item adding and GridBag constraints:

I know I could just do constraints beforehand and add as normal, but it seems to get the job done anyway so I left it. However, this is clearly not doing the trick... now, if I change it around, it still does nothing. Here is a picture of my program:

If there is any more information needed, I'd be happy to provide... I've been at this for hours now trying to make it all look right and I just can't. Any tips? Can't I just say, "Hey, I want the column to be this big, NO BIGGER, and button, I want you to fill up that column, even if the text overlaps." Isn't there ANY WAY to work this around so I can get a static look for my program rather than having to see what it does at runtime? I heard I shouldn't use setPreferredSize(), so I don't plan to, but it seems like my columns are just picking whatever sizes they should and I don't get how to effectively position everything to make it look... well, good.

Thanks guys.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Try to create Box by Box.createHorizontalBox() and add your buttons to it, then try to align that Box the way you want
Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
A question about the boxes... if I create a box, should I put it in the bottom-left square of the GridBagLayout and give it the width of all the columns so it can stretch, or what?
Brian Drelling
Ranch Hand

Joined: Oct 02, 2009
Posts: 42
Sorry to double-post... not sure if there are rules on that, but on the forum I moderate (on another website, obviously), we usually have a ten-minute rule for double-posting and editing, so I hope something like that applies here in terms of spam, if at all...

Anyway, the box worked GREAT! How can I space my buttons out within the box, though? Like can I give them a bit of padding somehow?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

You should have started a new post for your question.
I am locking this thread. In case you need to discuss this issue further, please start a new thread.

Check this out for tips on how to use BoxLayout There is a section which talks about using the Box class and its several static methods which you can use to adjust the spacing.

In such a scenario, I personally would prefer to use a JPanel (which defaults to FlowLayout). The JComponent#setPreferredSize() would give me control over all the button sizes.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: Trouble Aligning JButtons (And setting width)
It's not a secret anymore!