This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Trouble Aligning JButtons (And setting width)

 
Andrew Ceronoski
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello:

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
Bartender
Pie
Posts: 3223
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
 
Paul Clapham
Sheriff
Pie
Posts: 20194
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
You should have started a new post for your question.
http://faq.javaranch.com/java/UseOneThreadPerQuestion
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 http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html. 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.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic