• Post Reply 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JButton size

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
I have an array of buttons and I add all buttons to Horizontal Box. Then I add Box to JPanel which is BorderLayout. But sizes of all buttons vary according to their text.
I tried setPreferredsize, setMaximumsize,setMininmumsie. but no use.
As BorderLayout doesnt respect preferred component size, I also tried the same with FlowLayout manager. still doesnt work.
ne idea how can i make all buttons of the same size?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you add the buttons to the Box, and BoxLayout respects their preferred sizes, you should do that first. The Box and BoxLayout are in charge of getting all the buttons the same size. At that point you can add it to your JPanel with very little problem. Just pack() the Box and you should be okay.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GridLayout will make all the buttons equal size... you may want to use something other than BorderLayout to hold the panel that holds the buttons, though... or you will end up with GIANT buttons! (Probably just setPreferredSize() on the panel that holds the buttons and use FlowLayout on the outer panel.)
 
Kamlesh Sangani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no i am not using box layout. I m using Box (i.e. horizontal n vertical boxes). and i can not find pack() method in it.
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API docco:


Box uses a BoxLayout object as its layout manager

 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a little test program. I found I needed all three setSize methods for the buttons to stay at their preferred size.
 
reply
    Bookmark Topic Watch Topic
  • New Topic