Grid Bag Layout
Objective #1: What is a Grid Bag Layout.
- A Grid Bag Layout is the layout that allows you the greatest flexibility in design without having to manually place each component.
- FROM AUTHOR OF THIS PAGE DYLAN HOGUE(A Grid Bag Layout is more for making things look pretty and more user friendly, if you are going to use this then I suggest you get your program working with a regular flowlayout then make a copy and modify the copy to use this. That way you will have something to fall back on if you can't get this working. (Not going to lie, this isn't the easiest thing to do and does take an open mind, patience, and time to understand (Truthfully it toke me about 20-40 hours to understand this as well as i do (only toke about 10 hours to get it working(not the way i wanted it to)))))
- A grid bag layout, as the first part of it's name suggests, is like a grid layout but is much more customizeable. With a grid bag layout you can do many things which you couldn't with the grid layout.
- For Example:
- specify length of each different column
- specify height of each different row
- specify how many columns each component takes up
- specify how many rows each component takes up
- many other things which i will hit on later(don't think this is an all inclusive guide, you kids need to learn to find some things by yourselves)
Objective #2: How to impliment a Grid Bag Layout
- First you must start by adding the following import statements: import java.awt.GridBagConstraints; AND import java.awt.GridBagLayout;
- Next you must initilize your GridBagConstraints object using the following line: GridBagConstraints c = new GridBagConstraints();
- Then you must initilize your GridBagConstraints
- What are GridBagConstraints???
- These are like the guidelines which your layout requires inorder to correctly place the component.
- They consist of quite a few different properties (some which are listed and explained in the next couple of bullets)
- fill - when the component being placed in the specific grid space is smaller then the area of the grid space, this property will tell whether to resize the component and if so then how(horizontal, vertical, both, or none)
- weightx/weighty - specifies how to distribute extra horizontal/vertical space between the columns(not to sure on this one, look up in api(never had extra space so always just left it at a value of .5))
- anchor - tells where in the grid space the component should be placed(imagine one grid space as a tic-tac-toe board and your trying to decide where to place your X)(values are from top left and on as follows: FIRST_LINE_START, NORTH, FIRST_LINE_END, LINE_START, CENTER, LINE_END, LAST_LINE_START, SOUTH, LAST_LINE_END(can also use northeast, southwest, etc.)
- ipadx/ipady - this is the internal padding, it tells how many pixels should be added to the components minimumsize
- gridwidth - this tells how many columns a single component should take up
- gridheight - this tells how many rows a single component should take up
- gridx - the x position in the grid (0 to grid size - 1)
- gridy - the y position in the grid (0 to grid size - 1)
- columnWidths - allows you to set the width of each column (set equal to an int[] <- each one is the width of a seperate column)
- rowHeights - allows you to set the height of each row (set equal to an int[] <- each one is the height of a seperate row)
- (TO SEE ANY THAT I MAY HAVE MISSED LOOK AT THE API)
- To add a new object to the layout you change the grid bag contraints object to how you want it.
- You then simply (on your applet/frame class) call the following statement: this.add(*component*, c);