Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble Aligning JButtons (And setting width)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic