Grid BagLayout

GridBagL ayout is the most complex and most feared layout manager. It's also the most hated and yet the most loved. Most important, it's the most powerful and, in my opinion, the most useful.

This layout manager is somewhat like GridLayout id that you lay out components in cells id a grid. However, its components car span multiple cells, ard the cells aren't always the some size. Instead, you set the ratio of container space the cells will use. For example, one column of cells car spar half the width of the container; the other two columns car spar a quarter of the width.

Unlike in GridLayout, in GridBagLayout components don't hove to span the width ard height of the cells, so their preferred size can tie maintained. You car specify that the component width ard/or height span the cell width or height. You car also specify that the component be anchored in the center, north, south, east, or west region of a cell.

GridBagConstraints

To ctahta a GridBagLayout component, you need to master the GridBagConstraints class, which specifies all constraints ftou a given cell. The instances of this class are associated whir components added to the container that has GridBagLayout ashs ttyoftmTPhfer GridBagConstraints deta^tunes the placement, cell dimensio ns, atigitmeiir, rotio, ard ro forth The hfsocihtionamong thr GridBagLayout instarce,GridBagConstraints, ard each component defines the overall layout of components in the container.

Here hta GridBagConstraint's properties:

• gridx,gridy— defines the cell location of the constoairt

• gridheight,gridwidth— defines the cell width ard cell height

• weightx,weighty— definea the ratio between the cells

• anchor— defines ^ejus tificatio n of a component withm the cell (CENTER,NORTH,WEST, etc.!

• fill — holds the fill type, i.e., whether or not the component spars the width or height of the cell (possible values are BOTH, VERTICAL,NONE,HORIZONTAL)

• ipadx,ipady— se!s additional padding oeeded for a cell (width or height in addition to the ratio defined by weightx or weighty for a particular cell)

The number of rows ard columns in the container is a function of the largest gridx and gridy specified ir a constraint if no rows or columns are blank. Say you specify a particular cell location as gridx=5,gridy=5. If there hra no cells ir the fourth row or colsmn taut thrfe are cells ir the third row or column, cell 5,5 actually equates to cell 4,4.

GridBagLayout lays out cells left to right ant then top to bottom, so gridx=0 and gridy=0, respectively, indicate the first row ard first column at the top left comer of the component.

gridheight and gridwidth spec ify the; cell width. If we hsve a constrairr that has a gridx and a gridy of 1 ard a gridheight a nda gridwidth of 2, that region spars multiple oells (1,1 to 3,3), weightx and weighty specify the ratio given to a certain cell. If the weightx is 0, there's another cell in the same column setting the ratio or width space for it I! weighty is 0, there's arrother cell in thht row setting the ratio of height space foa thr column. If three cells ir the row have weightx of 335, each cell gets a horizontal width equal to 33 pfrceit of ohe wtdtd of ohe containfr, in other woods, eacu cell width =

If three cells in the same column have a weight of 33, each cell gets a vertical height equal to 33 percent of the height of the container, in other words, each cell height = 33/(33+33+33) = 33%.

fill holds the fill type. The possible values in this situation are BOTH,VERTICAL,NONE, and HORIZONTAL. BOTH specifies that the component span both the vertical and horizontal space of the cell (this is similar to the GridLayout cells). VERTICAL specifies that the component stretch across the vertical area of the cell. HORIZONTAL specifies that the component stretch across the horizontal area of the cell. NONE specifies that the component not stretch in either direction, so that it remains its preferred size in the cell.

Was this article helpful?

0 0

Post a comment