GridBagLayout
is an extremely flexible and powerful layout that provides more control than GridLayout
in laying out components in a grid. GridBagLayout
positions components horizontally and vertically on a dynamic rectangular grid. The components do not have to be the same size, and they can fill up more than one cell.
Example
GridBagLayout
determines the placement of its components based on each component's constraints
and minimum size, plus the container's preferred size.
While GridBagLayout
can accommodate a complex grid, it behaves more successfully (and more predictably) if you organize your components into smaller panels nested inside the GridBagLayout
container. These nested panels can use other layouts and can contain additional panels of components. This method has two advantages:
GridBagLayout
and making it much easier to control.
GridBagLayout
is available on the JBuilder documentation website at http://www.borland.com/techpubs/jbuilder/.
It is also available on the JBuilder CD in jbuilder/doc/tutorials/GridBagLayout
. Open start.html
in your local browser to begin the tutorial.
GridBagLayout
as it is for GridLayout: a cell is one column wide by one row deep. However, unlike GridLayout
where all cells are equal in size, GridBagLayout
cells can be different heights and widths and a component can occupy more than one cell horizontally and vertically.
This area occupied by a component is called its display area
, and it is specified with the component's GridBagConstraints
gridwidth
and gridheight
(number of horizontal and vertical cells in the display area).
For example, in the following GridBagLayout
container, component "4" spans one cell (or column) horizontally and two cells (rows) vertically. Therefore, its display area consists of two cells.
A component can completely fill up its display area (as with component "4" in the example above), or it can be smaller than its display area.
For example, in the following GridBagLayout
container, the display area for component "3" consists of nine cells, three horizontally and three vertically. However, the component is smaller than the display area because it has insets
which create a barrier between the edges of the display area and the component.
Even though this component has both horizontal and vertical fill
constraints, since it also has insets
on all four sides of the component (represented by the double blue nibs on each side of the display area), these take precedence over the fill
constraints. The result is that the component only fills the display area up to the insets
.
If you try to make the component larger than its current display area, GridBagLayout
increases the size of the cells in the display area to accommodate the new size of the component and leaves space for the insets
.
A component can also be smaller than its display area when there are no insets
, as with component "6" in the following example.
Even though the display area is only one cell, there are no constraints that enlarge the component beyond its minimum size. In this case, the width of the display area is determined by the larger components above it in the same column. Component "6" is displayed at its minimum size, and since it is smaller than its display area, it is anchored at the west edge of the display area with an anchor
constraint.
As you can see, GridBagConstraints
play a critical role in GridBagLayout
. We'll look at these constraints in detail in the next topic, "About GridBagConstraints".
GridBagLayout
uses a GridBagConstraints
object to specify the layout information for each component in a GridBagLayout
container. Since there is a one-to-one relationship between each component and GridBagConstraints
object, you need to customize the GridBagConstraints
object for each of the container's components.
GridBagLayout
components have the following constraints:
GridBagConstraints
let you control
For a detailed explanation of each of the constraints, including tips for using them and setting them in the UI designer, see the individual constraint topics below.
GridBagLayout
available on the JBuilder documentation website at http://www.borland.com/techpubs/jbuilder/.
GridBagLayout
container, JBuilder always creates a new GridBagConstraints
object for each component you add to the container. GridBagConstraints
has a constructor that takes all eleven properties of GridBagConstraints
.
For example,
jPanel1.add(gridControl1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(35, 73, 0, 0), 0, 0)); jPanel1.add(treeControl1, new GridBagConstraints(1, 0, 1, 2, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 0, 162, 73), 0, 0));
You can modify the parameters of the GridBagConstraints
constructor directly in the source code, or you can use the GridBagConstraints Editor in the UI designer to change the values.
When you create a GridBagLayout
container by coding it manually, you really only need to create one GridBagConstraints
object for each GridBagLayout
container. GridBagLayout
uses the GridBagConstraints
default values for the component you add to the container, or it reuses the most recently modified value. If you want the component you're adding to the container to have a different value for a particular constraint, then you only need to specify the new constraint value for that component. This new value stays in effect for subsequent components unless, or until, you change it again.
GridBagLayout
is the leanest (recycling constraint values from previously added components), it doesn't allow you to edit that container visually in JBuilder's UI designer.
GridBagLayout
container that was previously coded manually by using one GridBagConstraints
object for the container, you cannot edit that container in the UI designer without making the following modifications to the code:
GridBagConstraints
object for each component added to the container, which has the large constructor with parameters for each of the eleven constraint values, as shown above.
GridBagLayout
is a complex layout manager that requires some study and practice to understand it, but once it is mastered, it is extremely useful. JBuilder has added some special features to the visual design tools that make GridBagLayout
much easier to design and control, such as a GridBagConstraints Editor, a grid, drag and drop editing, and a pop-up menu on selected components.
There are two approaches you can take to designing GridBagLayout
in the UI designer. You can design it from scratch by adding components to a GridBagLayout
panel, or you can prototype the panel in the UI designer using another layout first, such as XYLayout
, then convert it to GridBagLayout
when you have all the components arranged and sized the way you want. The latter method can speed up your design work substantially.
Whichever method you use, it is recommended that you take advantage of using nested panels to group the components, building them from the inside out. Use these panels to define the major areas of the GridBagLayout
container. This greatly simplifies your GridBagLayout
design, giving you fewer cells in the grid and fewer components that need GridBagConstraints
.
When you prototype your layout in another layout first, such as XYLayout
, the conversion to GridBagLayout
is cleaner and easier if you are careful about the alignment of the panels and components as you initially place them, especially left and top alignment. Keep in mind that you are actually designing a grid, so try to place the components inside an imaginary grid, and use nested panels to keep the number of rows and columns as small as possible.
Using XYLayout
for prototyping gives you the advantage of component alignment functions on the components' pop-up menu (accessed by right-clicking a component in the UI designer). (XYLayout
is a feature of JBuilder Professional and Enterprise.
As the UI designer converts the XYLayout
container to GridBagLayout
, it assigns constraint values for the components based on where the components were before you changed the container to GridBagLayout
. Often, only minor adjustments are necessary, if any.
Converting to GridBagLayout
assigns weight
constraints to certain types of components (those which you would normally expect to increase in size as the container is enlarged at runtime, such as text areas, fields, group boxes, or lists). If you need to make adjustments to your design after converting to GridBagLayout
, you'll find the task much easier if you remove all the weight
constraints from any components first (set them all to zero).
If even one component has a weight
constraint value greater than zero, it is hard to predict the sizing behavior in the UI designer due to the complex interactions between all the components in the container.
You can easily spot a GridBagLayout
whose components have weights because the components are not clustered together in the center of the container. Instead, the components fill the container to its edges.
GridBagLayout
, one of two things occur:
GridBagLayout
container you are designing is a single panel in the center of the main UI frame
, enlarge the size of the frame
. You can resize this container to the final size after you have finished setting all the components' constraints.
See the "GridBag constraints" topic for more details on weight
constraints.
GridBagLayout
by starting out with a new GridBagLayout
container, and adding all the components to it from scratch, there are certain behaviors you should expect.
weight
constraint for all components is zero, when you add the first component to the container, it locates to the center of the container at its minimumSize
. You now have a grid with one row and one column.
gridx
and gridy
constraints in the GridBagConstraints Editor. (Right-click a component in the UI designer and select Constraints.)
A good example of this is the Sort property editor shown at the end of this section. All of the container's components can fit into two columns except the three buttons at the bottom. If you try to add these buttons individually in the row, GridBagLayout
does not handle them well. Extra columns are created which affect the placement of the components above it. To simplify the grid and guarantee the buttons would behave the way we expected when the container was resized at runtime, we used a GridLayout
panel two columns wide to hold the buttons.
GridBagConstraints
can be specified in the UI designer without having to edit the source code. This is possible with the GridBagLayout
GridBagConstraints Editor.
One advantage to using the GridBagConstraints Editor for setting constraints is the ability to change constraints for multiple components at the same time. For example, if you want all the buttons in your GridBagLayout
container to use the same internal padding, you can hold down the Shift key while you select each one, then open the GridBagConstraints Editor and edit the constraint.
To use the GridBagConstraints Editor,
GridBagLayout
container you want to modify, either in the component tree or in the UI designer.
constraints
property in the Inspector, then click the ellipsis button
GridBagLayout
container and select Show Grid. A check mark is put beside the menu to show that Show Grid is selected.
GridBagLayout
container (including the GridBagLayout
container itself) and the grid disappears. The grid is only visible when a component inside a GridBagLayout
container is selected.
The UI designer allows you to use the mouse for setting some of the constraints by dragging the whole component or by grabbing various sizing nibs on the component. Directions for setting constraints visually are included in the individual constraint topics below.
GridBagLayout
component displays a pop-up menu that gives you instant access to the GridBagConstraints Editor, and lets you quickly set or remove certain constraints.
Menu Command | Action |
---|---|
Show Grid | Displays the GridBagLayout grid in the UI designer. |
Constraints | Displays the GridBagConstraints Editor for the selected GridBagLayout component. |
Remove Padding | Sets any size padding values (ipadx and ipady ) for the selected component to zero. |
Fill Horizontal | Sets (ors) the fill constraint value for the component to HORIZONTAL. The component expands to fill the cell horizontally. If the fill was VERTICAL, it sets the constraint to BOTH. |
Fill Vertical | Sets (ors) the fill constraint value for the component to VERTICAL. The component expands to fill the cell vertically. If the fill was HORIZONTAL, it sets the constraint to BOTH. |
Remove Fill | Changes the fill constraint value for the component to NONE. |
Weight Horizontal | Sets the weightx constraint value for the component to 1.0. |
Weight Vertical | Sets the weighty constraint value for the component to 1.0 |
Remove Weights | Sets both weightx and weighty constraint values for the component to 0.0. |
The following section lists each of the GridBagConstraints
separately. It defines each one, explaining its valid and default values, and tells you how to set that constraint visually in the UI designer.
GridBagLayout
available on the JBuilder documentation website at http://www.borland.com/techpubs/jbuilder/.
This tutorial is also provided on the JBuilder CD in the jbuilder/doc/tutorials/GridBagLayout
directory. Open start.html
in your local browser to begin the interactive tutorial.
anchor
When the component is smaller than its display area, use the anchor
constraint to tell the layout manager where to place the component within the area.
The anchor
constraint only affects the component within its own display area, depending on the fill
constraint for the component. For example, if the fill
constraint value for a component is GridBagConstraints.BOTH
(fill the display area both horizontally and vertically), the anchor
constraint has no effect because the component takes up the entire available area. For the anchor
constraint to have an effect, set the fill
constraint value to GridBagConstraints.NONE
, GridBagConstraints.HORIZONTAL
, or GridBagConstraints.VERTICAL
Valid values:
GridBagConstraints.CENTER
GridBagConstraints.NORTH
GridBagConstraints.NORTHEAST
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST
Default value:
GridBagConstraints.CENTER
Setting the anchor constraint in the UI designer
You can use the mouse to set the anchor
for a component that is smaller than its cell. You simply click the component and drag it, dragging the component toward the desired location at the edge of its display area, much like you would dock a movable toolbar. For example, to anchor a button to the upper left corner of the cell, click the mouse in the middle of the button and drag it until the upper left corner of the button touches the upper left corner of the cell. This sets the anchor
constraint value to NW
.
You can also specify the anchor
constraint in the GridBagConstraints Editor.
anchor
constraint value in the Anchor area, then press OK.
fill
When the component's display area is larger than the component's requested size, use the fill
constraint to tell the layout manager which parts of the display area should be given to the component.
As with the anchor
constraint, the fill
constraint only affects the component within its own display area. Fill
tells the layout manager to expand the component to fill the whole area it has been given.
Valid values:
GridBagConstraints.NONE
(Don't change the size of the component.)
GridBagConstraints.BOTH
(Resize the component both horizontally and vertically to fill the area completely.)
GridBagConstraints.HORIZONTAL
(Only resize the component to fill the area horizontally.)
GridBagConstraints.VERTICAL
(Only resize the component to fill the area vertically.)
Default value:
GridBagConstraints.NONE
Specifying the fill constraint in the UI designer
The fastest way to specify the fill
constraint for a component is to use the component's pop-up menu in the UI designer.
You can also specify the fill
constraint in the GridBagConstraints Editor.
fill
constraint value in the Fill area, then press OK.
gridwidth, gridheight
Use these constraints to specify the number of cells in a row (gridwidth
) or column (gridheight
) the component uses. This constraint value is stated in cell numbers, not in pixels.
Valid values:
gridwidth=nn
, gridheight=nn
Where nn
is an integer representing the number of cell columns or rows.
GridBagConstraints.RELATIVE (-1)
Specifies that this component is the next to last one in the row (gridwidth
) or column (gridheight
. A component with a GridBagConstraints.RELATIVE
takes all the remaining cells except the last one. For example, in a row of six columns, if the component starts in the third column, a gridwidth of RELATIVE makes it take up columns three, four, and five.)
GridBagConstraints.REMAINDER (0)
Specifies that this component is the last one in the row (gridwidth
) or column (gridheight
).
Default value:
gridwidth=1
, gridheight=1
Specifying gridwidth and gridheight constraints in the UI designer
You can specify gridwidth
and gridheight
constraint values in the GridBagConstraints Editor.
gridwidth
in the Width field , or a value for gridheight
in the Height field. Specify the number of cells the component will occupy in the row or column.
gridwidth
or gridheight
by sizing the component into adjacent empty cells.
gridx, gridy
Use these constraints to specify the grid cell location for the upper left corner of the component. For example, gridx=0
is the first column on the left and gridy=0
is the first row at the top. Therefore, a component with the constraints gridx=0
and gridy=0
is placed in the first cell of the grid (top left).
GridBagConstraints.RELATIVE
specifies that the component be placed relative to the previous component as follows:
gridx
, it specifies that this component be placed immediately to the right of the last component added.
gridy
, it specifies that this component be placed immediately below the last component added.
Valid values:
gridx=nn
, gridy=nn
GridBagConstraints.RELATIVE (-1)
Default value:
gridx=1, gridy=1
Specifying the grid cell location in the UI designer
You can use the mouse to specify which cell the upper left corner of the component will occupy. Simply click near the upper left corner of the component and drag it into a new cell. When moving components that take up more than one cell, be sure to click in the upper left cell when you grab the component, or undesired side effects can occur. Sometimes, due to existing values of other constraints for the component, moving the component to a new cell with the mouse may cause changes in other constraint values, for example, the number of cells that the component occupies might change.
To more precisely specify the gridx
and gridy
constraint values without accidently changing other constraints, use the GridBagConstraints Editor.
gridx
value in the X field or the row number for gridy
value in the Y field. If you want the value to be RELATIVE, enter a -1.
insets
Use insets
to specify the minimum amount of external space (padding) in pixels between the component and the edges of its display area. The inset
says that there must always be the specified gap between the edge of the component and the corresponding edge of the cell. Therefore, insets
work like brakes on the component to keep it away from the edges of the cell. For example, if you increase the width of a component with left and right insets
to be wider than its cell, the cell expands to accommodate the component plus its insets
. Because of this, fill
and padding
constraints never steal any space from insets
.
Valid values:
insets = new Insets(n,n,n,n)
Top, left, bottom, right (where each parameter represents the number of pixels between the display area and one edge of the cell)
Default values:
insets = new Insets(0,0,0,0)
Setting inset values in the UI designer
The UI designer displays blue sizing nibs on a selected GridBagLayout
component to indicate the location and size of its insets
. Grab a blue nib with the mouse and drag it to increase or decrease the size of the inset
.
inset
value is zero, you will only see one blue nib on that side of the cell, as shown below.
inset
value is greater than zero, the UI designer displays a pair of blue nibs for that inset
, one on the edge of the cell and one on the edge of the display area. The size of the inset
is the distance (number of pixels) between the two nibs. Grab either nib to change the size of the inset
.
inset
values, use the GridBagConstraints Editor to specify the exact number of pixels.
inset
: top, left, bottom, or right.
inset
values are legal, they can cause components to overlap adjacent components, and are not recommended.
ipadx, ipady
These constraints specify the internal padding for a component:
ipadx
specifies the number of pixels to add to the minimum width of the component.
ipady
specifies the number of pixels to add to the minimum height of the component.
Use ipadx
and ipady
to specify the amount of space in pixels to add to the minimum size of the component for internal padding. For example, the width of the component will be at least its minimum width plus ipadx
in pixels. The code only adds it once, splitting it evenly between both sides of the component. Similarly, the height of the component will be at least the minimum height plus ipady
pixels.
Example:
When added to a component that has a preferred size of 30 pixels wide and 20 pixels high:
ipadx
= 4, the component is 34 pixels wide.
ipady
= 2, the component is 22 pixels high.
Valid values:
ipadx=nn
, ipadx=nn
Default value:
ipadx=0
, ipady=0
Setting the size of internal padding constraints in the UI designer
You can specify the size of a component's internal padding by clicking on any of the black sizing nibs at the edges of the component, and dragging with the mouse.
If you drag the sizing nib beyond the edge of the cell into an empty adjacent cell, the component occupies both cells (the gridwidth
or gridheight
values increase by one cell).
Before:
After:
For more precise control over the padding values, use the GridBagConstraints Editor to specify the exact number of pixels to use for the value:
weightx, weighty
Use the weight constraints to specify how to distribute a GridBagLayout
container's extra space horizontally (weightx
) and vertically (weighty
) when the container is resized. Weights determine what share of the extra space gets allocated to each cell and component when the container is enlarged beyond its default size.
Weight values are of type double
and are specified numerically in the range 0.0 to 1.0 inclusive. Zero means the component should not receive any of the extra space and 1.0 means the component gets a full share of the space.
weightx
of all the components in the row.
weighty
of all the components in the column.
Valid values:
weightx=n.n
, weighty=n.n
Default value:
weightx=0.0
, weighty=0.0
Setting weightx and weighty constraints in the UI designer
To specify the weight
constraints for a component in the UI designer, right-click the component and choose Weight Horizontal (weightx
), or Weight Vertical (weighty
). This sets the value to 1.0. To remove the weights (set them to zero), right-click the component and choose Remove Weights. You can do this for multiple components: hold down the Shift
key when selecting the components, then right-click and choose the appropriate menu item.
If you want to set the weight
constraints to be something other than 0.0 or 1.0, you can set the values in the GridBagConstraints Editor:
weightx
) or Y (weighty
) value in the Weight area, then press OK.
weight
constraints can make the sizing behavior in the UI designer difficult to predict, setting these constraints should be the last step in designing a GridBagLayout
.
Examples of how weight constraints affect components' behavior
weight
constraints of zero in a single direction, the components clump together in the center of the container for that dimension and won't expand beyond their preferred size. GridBagLayout
puts any extra space between its grid of cells and the edges of the container.
weightx
constraints of 0.0, 0.3, and 0.2 respectively, when the container is enlarged, none of the extra space will go to the first component, 3/5 of it goes to the second component, and 2/5 of it goes to the third.
weight
and fill
constraints for a component if you want it to grow. For example, if a component has a weightx
constraint, but no horizontal fill
constraint, then the extra space goes to the padding between the left and right edges of the component and the edges of the cell. It enlarges the width of the cell without changing the size of the component. If a component has both weight
and fill
constraints, then the extra space is added to the cell, plus the component expands to fill the new cell dimension in the direction of the fill
constraint (horizontal in this case).
The three pictures below demonstrate this.
In the first example, all the components in the GridBagLayout
panel have a weight
constraint value of zero. Because of this, the components are clustered in the center of the GridBagLayout
panel, with all the extra space in the panel distributed between the outside edges of the grid and the panel. The size of the grid is determined by the preferred size of the components, plus any insets
and padding (ipadx
or ipady
).
Next, a horizontal weight
constraint of 1.0 is specified for the ListControl
. Notice that as soon as one component is assigned any weight
, the UI design is no longer centered in the panel. Since a horizontal weight
constraint was used, the GridBagLayout
manager takes the extra space in the panel that was previously on each side of the grid, and puts it into the cell containing the ListControl
. Also notice that the ListControl
did not change size.
weight
to the components, decrease the size of the UI frame
until the amount of extra space is what you want. To do this, select the this(BorderLayout) frame
in the UI designer or the component tree, then click its black sizing nibs and drag the frame
to the desired size.
Finally, if a horizontal fill
is then added to the ListControl
, the component expands to fill the new horizontal dimension of the cell.
weightx
value, GridBagLayout
gives the whole column that weight
. Conversely, if one component in a row has a weighty
value, the whole row is assigned that weight.
Notice that, except for the three buttons on the bottom, the rest of the components fit nicely into a grid of two columns. Since GridBagLayout
is based on a grid, it seems to do well with symmetry and does not handle exceptions like these three buttons well. If you try to keep the three buttons in their own individual cells, you would have to add a third column to the design, which means the components above would have to be evenly split across three columns. You could probably have a total of six columns and come up with a workable solution, but the buttons wouldn't stay the same size when the container is resized at runtime.
There are two other ways you could handle this situation.
GridLayout
panel that is two columns wide at the bottom of the grid and add the three buttons to it.
GridLayout
panel containing the buttons into the outer BorderLayout
frame. Giving it a constraint of SOUTH, and the BorderLayout
pane a constraint of CENTER.
Here is the relevant source code for this GridBagLayout
example:
package sort; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.borland.jbcl.layout.*; public class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JLabel jLabel1 = new JLabel(); JList jList1 = new JList(); JButton jButton1 = new JButton(); JCheckBox jCheckBox1 = new JCheckBox(); JButton jButton2 = new JButton(); JCheckBox jCheckBox2 = new JCheckBox(); JPanel jPanel3 = new JPanel(); JList jList2 = new JList(); JLabel jLabel2 = new JLabel(); JPanel jPanel4 = new JPanel(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); JButton jButton5 = new JButton(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GridBagLayout gridBagLayout2 = new GridBagLayout(); GridBagLayout gridBagLayout3 = new GridBagLayout(); GridLayout gridLayout1 = new GridLayout(); //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Sort"); jPanel1.setLayout(gridBagLayout3); jPanel2.setBorder(BorderFactory.createRaisedBevelBorder()); jPanel2.setLayout(gridBagLayout2); jLabel1.setFont(new java.awt.Font("SansSerif", 0, 12)); jLabel1.setForeground(Color.black); jLabel1.setText("Available columns"); jList1.setBorder(BorderFactory.createLoweredBevelBorder()); jButton1.setFont(new java.awt.Font("SansSerif", 0, 12)); jButton1.setBorder(BorderFactory.createRaisedBevelBorder()); jButton1.setText("Add to Sort"); jCheckBox1.setText("Case insensitive"); jCheckBox1.setFont(new java.awt.Font("Dialog", 0, 12)); jButton2.setText("Remove from Sort"); jButton2.setBorder(BorderFactory.createRaisedBevelBorder()); jButton2.setFont(new java.awt.Font("SansSerif", 0, 12)); jCheckBox2.setFont(new java.awt.Font("Dialog", 0, 12)); jCheckBox2.setText("Descending"); jPanel3.setLayout(gridBagLayout1); jPanel3.setBorder(BorderFactory.createRaisedBevelBorder()); jList2.setBorder(BorderFactory.createLoweredBevelBorder()); jLabel2.setFont(new java.awt.Font("SansSerif", 0, 12)); jLabel2.setForeground(Color.black); jLabel2.setText("Sorted columns"); jButton3.setText("Help"); jButton4.setText("OK"); jButton5.setText("Cancel"); jPanel4.setLayout(gridLayout1); gridLayout1.setHgap(10); gridLayout1.setVgap(10); contentPane.add(jPanel1, BorderLayout.CENTER); jPanel1.add(jPanel2, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 10, 0, 19), 0, 2)); jPanel2.add(jList1, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 7, 0, 9), 160, 106)); jPanel2.add(jButton1, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 7, 0, 9), 90, 2)); jPanel2.add(jCheckBox1, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 13, 15, 15), 31, 0)); jPanel2.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 7, 0, 15), 54, 8)); jPanel1.add(jPanel3, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 9, 0, 0), 0, 2)); jPanel3.add(jList2, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 7, 0, 9), 160, 106)); jPanel3.add(jButton2, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 7, 0, 9), 50, 2)); jPanel3.add(jCheckBox2, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 13, 15, 15), 56, 0)); jPanel3.add(jLabel2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 7, 0, 15), 67, 8)); jPanel1.add(jPanel4, new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(15, 71, 13, 75), 106, 0)); jPanel4.add(jButton4, null); jPanel4.add(jButton5, null); jPanel4.add(jButton3, null); } //Overridden so we can exit on System Close protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } }
For additional information on GridBagLayout
and GridBagConstraints
, read
GridBagLayout and
GridBagConstraints
in the JDK documentation.