• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton size  RSS feed

 
Kamlesh Sangani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • 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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!