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):||23 April 2018|
|PDF File Size:||19.36 Mb|
|ePub File Size:||7.14 Mb|
|Price:||Free* [*Free Regsitration Required]|
Examples and practices described in this page don’t take advantage of improvements introduced in later releases. Here is a picture gridbagglayout an example that uses GridBagLayout.
Alternatively, to compile and run the example yourself, consult the example index. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns javz have the gridbaglayouy width. Essentially, GridBagLayout places components in rectangles gribdaglayout in a grid, and then uses the components’ preferred sizes to determine how big the cells should be.
The following figure shows the grid for the preceding applet. As you can see, the grid has three rows and three columns. The button in the second row spans all the columns; the button in the third row spans the two right columns. If you enlarge the window as shown in the following figure, you will notice that the bottom row, which contains Button 5, gets all the new vertical space. The new horizontal space is split evenly among all the columns.
This resizing behavior is based on weights the program assigns to individual components in the Gridgaglayout. This behavior is also specified by the program.
The way the program specifies the size and position characteristics of its components is by specifying constraints for each component. The preferred approach to set constraints on a component is to use the Container.
The following code is typical of what goes in a container that uses gridbaglaayout GridBagLayout. You will see a more detailed example in the next section. As you might have guessed from the above example, it is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints.
However, it is recommended that you do not reuse GridBagConstraintsas this can very easily lead to you introducing subtle bugs if jaa forget to reset the fields for each new instance. You can set the following GridBagConstraints instance variables:.
GridBagLayout does not allow components to span gridbaglayotu rows unless the component is in the leftmost column or you have specified positive gridx and gridy values jaca the component. Here is a picture of how these values are interpreted in a container that has the default, left-to-right component orientation. Unless you specify at least one non-zero value for weightx or weightyall the components clump together in the center of their container.
This is because when the weight is 0.
Generally weights are specified with 0. Larger numbers indicate that the component’s row or column should get more space. For each column, the weight gridbaglyout related to the highest weightx specified for gridbaglayoutt component within that column, with each multicolumn component’s weight being split somehow between the columns the component is in. Similarly, each row’s weight is related to the highest weighty specified for a component within that row.
Extra space tends to go toward the rightmost column and bottom row. The next section discusses constraints in depth, in the context of explaining how the example program works. The following code creates the GridBagLayout and the components it manages.
You can find the entire source file in GridBagLayoutDemo. This example uses one GridBagConstraints instance for all the components the GridBagLayout manages, however in real-life situations it is recommended that you do not reuse GridBagConstraintsas this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance.
Just before each component is added to the container, the code sets or resets to default values the appropriate instance variables in the GridBagConstraints object. It then adds the component to its container, specifying the GridBagConstraints object as the second argument to the add method. And before setting the constraints of the next component, the code resets the value of ipady to the default:. If a component’s display area is larger than the component itself, then you can specify whereabouts in the display area the component will be displayed by using the GridBagConstraints.
The anchor constraint’s values can be absolute north, south, east, west, and so onor orientation-relative at start of page, at end of line, at the start of the first line, and so onor relative to the component’s baseline. For a full list of the possible values of the anchor constraint, including baseline-relative values,see the API documentation for GridBagConstraints.
You can see gridbaglqyout the code extract above that Button 5 specifies that it should be displayed at the end of the display area by setting an anchor at GridBagConstraints. Here is a table that shows all the constraints for each component in GridBagLayoutDemo’s content pane.
Values jaava are not the default are marked in boldface. Values that are different from those in the previous table entry are marked in italics. GridBagLayoutDemo has two components that span multiple columns buttons 4 and 5.
To make button 4 tall, we added internal padding ipady to it. To put space between buttons 4 and 5, grivbaglayout used insets to add a minimum of 10 pixels above button 5, and we made button 5 hug the bottom edge of its cell.
All the components in the pane container are as wide as possible, given the cells that they occupy. If the program did not specify the fill, the buttons would be at their natural width, like this:. When you enlarge GridBagLayoutDemo’s window, the columns gridbaglayojt proportionately.
The actual value of these components’ weightx is unimportant. What matters is that all the components, and consequently, all the columns, have an equal weight that is greater than 0.
Using GridBagConstraints : GridBagConstraints « Swing « Java Tutorial
If no component managed by the GridBagLayout had weightx set, then when the components’ container was made wider, the components would stay clumped together in jvaa center of the container, like this:. If the container is given a size that is smaller or bigger than the prefered size, then any space is distributed according to the GridBagContainer weights.
Note that if gridbaglayotu enlarge the window, the last row is the only one that gets taller. This is because only button 5 has weighty greater than zero.
Learning Java, 4th Edition by Daniel Leuck, Patrick Niemeyer
Instead of invoking methods on a GridBagConstraints object, you manipulate its instance variables, as described in Grisbaglayout Constraints. You can find examples of using GridBagLayout throughout this tutorial. The following table lists a few. Laying Out Components Within a Container. This lesson covers writing layout code by hand, which can be challenging. If you are not interested in learning all gridbaglzyout details of layout management, you might prefer to use the GroupLayout layout manager combined with a builder tool to lay out your GUI.
Otherwise, if you want to code by hand and do not want to use GroupLayoutthen GridBagLayout is recommended as the next most flexible and powerful layout rgidbaglayout.
Set instance variables in the GridBagConstraints instance The following discussion assumes that the GridBagLayout controls a container that has a left-to-right component orientation. Previous releases require values named after points of the compass.
We recommend that you use the new constants, instead, since they enable easier localization. The Tutorial’s examples used to specify gridbaglaoyut constraints object a different way, which you might see in other programs as well.
Rather than specifying the constraints with the add method, our examples used to invoke the setConstraints method on the GridBagLayout object.
SWING GridBagLayout Class
How to Use FlowLayout Next page: How to Use GridLayout. Aligns two pairs of labels and text fields, plus adds a label across the full width of the container. How to Write a Container Listener. Positions five gridbaglajout within a container, using weights, fill, and relative positioning.