This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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