Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help understanding gridbaglayout  RSS feed

 
Sam Hackett
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a tough time understanding gridbaglayout. My intention with this code is to have a panel on the left side of the frame containing two buttons, one on top of the other and both centered vertically in the panel. I don't understand why when i use gridbagconstraints field gridy to change a components row, that is doesn't change anything. I think field anchor can be used to center components, but I'm not sure how to implement it. I've looked at resources explaining gridbag and it's just not sinking in so any help is appreciated.

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You need to set the layout of the panel otherwise the default layout is the FlowLayout.

Don't know if that is the only problem. Read the section from the Swing tutorial on How to Use GridBagLayout for working examples and explanations of all the constraints.
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Also find a copy of Horstmann and Cornell Core Java II vol I which has a section about grid bag, explaining why the GBC class is far better than the standard constraints class.
If you are using multiple panels and putting compinents on them, try a simpler layout for each panel, maybe Grid. Much easier.
 
Sam Hackett
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both. Setting the layout for the panel was the issue. I'll take a look at that book.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!