Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout stuff  RSS feed

 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have the following code:



I expect that the width of Button2 and Button 4 will be the same. However, the output is what you can see in the attached image.
What part of my code makes Button 2 and Button 4 wider than all other buttons?
Screenshot.jpg
[Thumbnail for Screenshot.jpg]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know, frankly. But I think everybody who struggles with GridBagLayout should take a look at SGLayout, an easier to use (IMO) layout that improves substantially on GridLayout.
 
Brett Spell
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:What part of my code makes Button 2 and Button 4 wider than all other buttons?


It's because Button 4's label text ("Long-Named Button 4") is wider than the others; the frame.pack() call essentially causes the buttons to be made just wide enough to be shown using their preferred sizes, which in turn is a function of the length of their labels. Button 2 is given the same width because it's in the same column as Button 4 and you told the GridBagLayout (when you included c.fill = GridBagConstraints.HORIZONTAL) that it should stretch Button 2 to fill the horizontal width of its column.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a static method?
When you have been through the tutorial at the top right of this link, find the more useful bits of the link. Read about the GBC class. There is a lot more about it in Horstmann's book (Core JavaII/vol 1 page 492-502 in my Java7 edition).
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Spell wrote:
Matan Bar Lev wrote:What part of my code makes Button 2 and Button 4 wider than all other buttons?


It's because Button 4's label text ("Long-Named Button 4") is wider than the others; the frame.pack() call essentially causes the buttons to be made just wide enough to be shown using their preferred sizes, which in turn is a function of the length of their labels. Button 2 is given the same width because it's in the same column as Button 4 and you told the GridBagLayout (when you included c.fill = GridBagConstraints.HORIZONTAL) that it should stretch Button 2 to fill the horizontal width of its column.


Thanks!
I saw that once the name of the Button is shorten the button becomes smaller. Good!

Still, when I keep the name of Button 4 "Button 4 Long Button" and add the line "c.fill = GridBagConstraints.NONE", I expect that Button 4 will NOT be wider that other buttons (maybe a certain part of its label will be discarded in order to fit to the width of other buttons). Well, in fact, it is NOT (See the attached image) what happens. How can I make sure that the length of the label doesn't affect the button size?




Screenshot001.jpg
[Thumbnail for Screenshot001.jpg]
 
Brett Spell
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:Thanks!


You're welcome.

Matan Bar Lev wrote:How can I make sure that the length of the label doesn't affect the button size?


You can't, at least not with a GridBagLayout; it's always going to use either the preferred size or the minimum size of the components it lays out. If you want the buttons to all be the same size / width then you should be using a GridLayout instead.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know there are lots of layouts described in the Java Tutorials? Also google for MigLayout; I have never used it but have heard that it is good.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!