GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows. Following is the declaration for gLayout class − public class GridBagLayout extends Object implements LayoutManager2, Serializable. Component; import ner; import gConstraints; import gLayout; import ; import JButton.
|Published (Last):||9 August 2018|
|PDF File Size:||15.19 Mb|
|ePub File Size:||12.20 Mb|
|Price:||Free* [*Free Regsitration Required]|
The constraints object specifies where a component’s display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout also considers each component’s minimum and preferred sizes in order to determine a component’s size. The overall orientation of the grid depends on the container’s ComponentOrientation property.
For horizontal left-to-right orientations, grid coordinate 0,0 is in the upper left corner of the container with x increasing to the right and y increasing downward.
For horizontal right-to-left orientations, grid coordinate 0,0 is in the upper right corner of the container with x increasing to the left and y increasing downward. To use a gridbaglwyout bag layout effectively, you must customize one or jaca of the GridBagConstraints objects that are associated with its components.
You customize a GridBagConstraints object by setting one or more of its instance variables: For horizontal left-to-right layout, a component’s leading corner is its upper left. For horizontal right-to-left layout, a component’s leading corner is its upper right.
java – How to Use GridBagLayout? – Stack Overflow
RELATIVE the default value to specify that the component be placed immediately following along the x axis for gridx or the y axis for gridy the component that was added to the container just before this component was added.
The default value is 1. REMAINDER to specify that the component’s display area will be from gridx to the last cell in the row for gridwidth or from gridy to the last cell in the column for gridheight.
RELATIVE to specify that gridnaglayout component’s display area will be from gridx to the next to the last cell in its row for gridwidth or from gridy to the next to the last cell in its column for gridheight.
Possible values are GridBagConstraints. BOTH make the component fill its display area entirely. The width of the gridbaglayyout will be at least its minimum width plus ipadx pixels. Similarly, the height of the component will be at least the minimum height plus ipady pixels. There are three kinds of possible values: Baseline relative values are calculated relative to the baseline.
Unless you specify a weight for at least one component in a row weightx and column weightyall the components clump together in the center of their container. This is because when the weight is zero the defaultthe GridBagLayout object puts any extra space between its grid of cells and the edges of the container.
If gridbaglayoyt of the components in the row has a valid baseline, the row does not have a baseline. The row that the component is aligned to is called the prevailing row. The following figure shows a baseline layout and includes a component that spans rows: This layout consists of three components: A panel that starts in row 0 and ends in row 1.
Because the second button and the panel share the same prevailing row, they are both javq along their baseline. Components positioned using one of the baseline-relative values resize differently than when positioned using an absolute or orientation-relative value. How components change is dictated by how the baseline of the prevailing row changes. The following rules dictate the resize behavior: Resizable components positioned above the baseline can only grow as tall as the baseline. For example, if the baseline is at and anchored at the top, a resizable component positioned above the baseline can never grow more than units.
Similarly, resizable components positioned below the baseline can only grow as high as the difference between the display height and the baseline. Resizable components positioned on the baseline with a baseline-resize behavior of OTHER are only resized if the baseline at the resized size fits within the display area.
If the baseline is such that it does not fit within the display area the component is not resized. If you position a component along the baseline, but the component does not have a valid baseline, it will be vertically centered in its space.
Similarly if you have positioned a component relative to the baseline and none of the components in the row have a valid baseline the component is vertically centered. The following figures show ten components all buttons managed by a grid bag layout. Figure 2 shows the layout for a horizontal, left-to-right container and Figure 3 shows the layout for a horizontal, right-to-left container. Horizontal, Left-to-Right Figure 3: In addition, the components have the following non-default constraints: Gridbbaglayout getLayoutOrigin Determines the origin of the layout area, in the graphics coordinate space of the target container.
String toString Returns a string representation of this grid bag layout’s values. Previously, this was the maximum number of grid grdbaglayout both horizontal and vertical gridbaglayoutt could be laid out by the grid bag layout. Current implementation doesn’t impose any limits on the size of a grid. The Keys in comptable are the components and the values are the instances of GridBagConstraints.
The information in this field is based on the most recent validation of the gridbag. If layoutInfo is null this indicates that there are no components in the gridbag or if there are components, they have not yet been validated.
If this field is non- null the values are applied to the gridbag after all of the minimum columns widths have been calculated. If columnWidths has more elements than gridbbaglayout number of columns, columns are added to the gridbag to match the number of elements in columnWidth.
If this field is non- null the values are applied to the gridbag after all of the minimum row heights have been calculated. If rowHeights has more elements than the number of rows, rowa are added to the gridbag to match the number of elements in rowHeights. If this field is non- null the values are applied gridvaglayout the gridbag after all of the columns weights have been calculated.
If columnWeights has more elements than the number of columns, the excess elements are ignored – they do not cause more columns to be created. If this field is non- null the values are applied to the gridbag after all of the rows weights have been calculated.
If rowWeights has more elements than the number of rows, the excess elements are ignored – they do not cause more rows to be created. A copy of the actual GridBagConstraints object is returned. The return value is not a copy, but is gridbsglayout actual GridBagConstraints object used by the layout mechanism. A comp value of null is invalid and returns null. This value represents the pixel coordinates of the top-left corner of the layout area regardless of the ComponentOrientation value of the container.
This is distinct from the grid origin given by the cell coordinates 0,0. Most applications do not call this method directly. Weights are used to calculate how much a given column or row stretches beyond its preferred size, if the layout has extra room to fill.
Each cell is identified by its column index ranging from 0 to the number of columns minus 1 and its row index ranging from 0 to the number of rows minus 1. The column index is returned as zero if x gridbaglayour to the left of the layout for a left-to-right container or to the right of the layout for a right-to-left container.
The column index is returned as the number of columns if x lies to the right of the layout in a left-to-right container or to the left in ggridbaglayout right-to-left container. The row index is returned as zero if y lies above the layout, and as the number of rows if y lies below the layout. The orientation of jjava container is determined by its ComponentOrientation property. Note that constraints are mutable and are, therefore, cloned jwva cached.
This specifies how jvaa component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0. This method reshapes components in gridvaglayout specified container in order to satisfy the contraints of this GridBagLayout object. This requires three passes through the set of children: Figure out the dimensions of the layout grid.
Griebaglayout which cells the components occupy. This also caches the minsizes for all the children when they are first encountered so subsequent loops don’t need to ask again.
This method should only be used internally by GridBagLayout. This method is the same as getLayoutInfo ; refer to getLayoutInfo for details on jav and return jafa.
SWING – GridBagLayout Class
This method is the same as adjustForGravity ; refer to adjustForGravity for details on parameters. This method is the same as getMinSize ; refer to getMinSize for details on parameters and return value. This method is the same as arrangeGrid ; refer to arrangeGrid for details on the parameter. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Use is subject to license terms. Also see the documentation redistribution policy. LayoutManagerLayoutManager2Serializable public class GridBagLayout extends Object implements LayoutManager2Serializable The GridBagLayout class is a flexible layout manager that aligns components vertically, horizontally or along their baseline without requiring that the components be of the same size.
Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area. This hashtable maintains the association between a component and its gridbag constraints.
This field holds a gridbag constraints instance containing the default values, so if a component does not have gridbag constraints associated with it, then the component will be assigned a gridbaglauout of the defaultConstraints. This field is no longer used to reserve arrays and keeped for backward compatibility. Adds the specified component to the layout, using the specified constraints object.
Adjusts the x, y, width, and height fields to the correct values depending on the constraint geometry and pads.
This method is obsolete and supplied for gridbaglayoyt compatability only; new code should call adjustForGravity instead. This method is obsolete and supplied for backwards compatability only; new code should call arrangeGrid instead. Fills in an instance of GridBagLayoutInfo for the current set of managed children. This method is obsolete and supplied for backwards compatability only; new code should call getLayoutInfo instead.
Determines the origin of the layout area, in the graphics coordinate space of the target container. Figures out the minimum size of the master based on the information from getLayoutInfo.
This method is obsolete and supplied for backwards compatability only; new code should call getMinSize instead. Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.