Designing a Basic Java Form Using the GridBag Customizer
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
This tutorial is the first in a two-part series that demonstrates how to design a simple Java form using the basic features of the NetBeans IDE GridBag Customizer. The series is intended as a guide to show how you can layout your GUI components without manually writing your layout code and then perform additional changes in an existing form to implement a specific target layout that is required by the project.
Each document in this series covers specific set of features.
-
Part 1: Designing a Basic Java Form Using the GridBag Customizer
-
Part 2: Designing an Advanced Java Form Using the GridBag Customizer
To follow this tutorial, you need the software and resources listed below.
Software or Resource | Version Required |
---|---|
An archive with the demo project containing the initial and target tutorial layouts. |
Notes:
-
You can download the project that is used as the starting point for this series as a
.zip
archive. -
This tutorial focuses on designing the layout of the container only. Adding functionality to the GUI is out of its scope.
Opening Example Project
-
Download and unzip the gbcustomizer-basic-tutorial.zip project to any location on your computer.
-
In the Projects tab, choose
File
>Open Project
, navigate to thegbcustomizer-basic-tutorial
project that you extracted in the previous step, and click Open Project. The project folder might be in a containing folder that is also calledgbcustomizer-basic-tutorial
. -
In the Reference Problem dialog box, click Resolve. The IDE automatically downloads the JUnit and JUnit 4 libraries. Follow the instructions in the NetBeans IDE installer to install the required plugins. When the installation is complete, click Finish.
-
Expand
Source Packages
>Tutorial
and double-clickContactsBasicInitial.java
. The sample form opens in the GUI Builder Design view.
GridBag Customizer Overview
The GridBag Layout Customizer is one of the most flexible and complex layout managers the Java platform provides. The Customizer 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 necessarily have the same width. Essentially, the GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
To display the GridBag Customizer, complete the steps below:
-
In the Design view, select the JFrame form.
-
Right-click the form and choose
Customize Layout
from the context menu. The Customize Layout dialog box opens as shown below.
In this tutorial the GridBagLayout is already set. In case you work with another form, in step 2 above, right-click the form and choose Set Layout > Grid Bag Layout from the context menu (this enables the Customize Layout menu item.). Then complete the procedure.
|
Grid Area
The Grid area is on the right side of the Customize Layout dialog box. It shows the grid layout of the components. The components in the sample form are already added but not laid out correctly.
Toolbar
A toolbar with five buttons is located above the Grid area. It provides convenient access to common commands, such as undoing, redoing, enabling uniform gaps, hiding empty rows and columns, and testing the layout.
Laying Out Components
The components for the ContactsBasicInitial
form are added and laid out in a single row. The GridBagLayout lays out the components like this when no layout constraints are specified.
Moving
You can move components using simple drag and drop as desired. The component is highlighted with green when selected. While dragging a component, its Grid X
and Grid Y
properties change to reflect its new position. New columns and rows are created automatically when needed.
To create a layout like shown in the picture below, move the components from columns 2 to 11 as follows:
-
Drag the
Surname:
label and the adjacent text field into the first two cells of the second row. -
Drag the
Street:
label, the adjacent text field, and the adjacentBrowse
button into the first three cells of the third row. -
Drag the
City:
label, the adjacent text field, and the adjacentBrowse
button into the first three cells of the fourth row. -
Drag the
State:
label and the adjacent combobox into the first two cells of the fifth row.
The components are now placed in accordance with the target layout.
When a component is moved the target cells are highlighted with green. |
Resizing
A component can be resized by dragging small square resize handles that appear around its perimeter when it is selected.
To resize the First Name:
and Surname:
text fields and make them occupy two adjacent cells, complete the steps below:
-
Control-click the two JTextField components to select them.
-
With both JTextFields selected, position the cursor over the cells right edge, click and drag until the orange highlighted guideline embraces the adjacent cells in column 2 on the right.
-
Release the cursor to resize the components.
The First Name:
and Surname:
text fields are extended to span between the two cells as shown in the following illustration. The occupied cells are highlighted.
Specifying Fill Layout Constraint
Though the First Name:
and Surname:
text fields occupy two cells, they have the preferred size and are placed in the middle of the display area. Before moving on, we need to fill out the whole area of the cells using the Fill
layout constraint.
To make the text fields wide enough to fill their display areas horizontally without changing their heights, in the Fill
combobox in the Property Sheet area, select Horizontal
.
Previewing
Now that you have successfully completed the ContactsBasicInitial
form layout, you can try your interface to see the results. You can preview your form as you work by clicking the Test Layout button () in the Customizer’s toolbar. The form opens in its own window, allowing you to test it prior to building and running.
The preview is useful to test dynamic behaviour of the layout, i.e. how the layout behaves when the designed container is resized.
Specifying Weight X and Weight Y Constraints
Specifying weights has a significant impact on the appearance of the GridBagLayout components. Weights are used to determine how to distribute space among columns (Weight X) and among rows (Weight Y); this is important for specifying resizing behavior. Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component’s row or column should get more space.
If you try to resize the previewed container horizontally, you can see that the layout components remain the same size and stay clumped in the middle of the container. Even the First Name:
and Surname:
fields that have the Fill constraint set to Horizontal do not grow since the Fill constraint refers to the cell inner area but not the cell’s size. In other words, a component with the Fill attribute set to a value different from none
claims that it "can" grow, but it does not claim that it "wants" to grow.
The Weight X and Weight Y layout constraints determine whether a component "wants" to grow in horizontal and vertical directions.
When two components in a row (or column) have a non-zero value of Weight X (or Weight Y) constraint the values determine how much the individual components grow. For example, if the values are 0.6 and 0.4 then the first component obtains 60% of the available additional space and the second component obtains 40%.
To make the designed container resize correctly in a horizontal direction, do the following:
-
Select the text field to the right of the
First Name:
label in the Grid Area of the GridBag Customizer. -
Type
1.0
in theWeight X
layout constraint value field and press Enter. -
Select the text field to the right of the
Surname:
label in the Grid Area of the GridBag Customizer. -
Type
1.0
in theWeight X
layout constraint value field and press Enter. -
Select the text field to the right of the
Street
label in the Grid Area of the GridBag Customizer. -
Select
Horizontal
in theFill
combobox and press Enter. -
Type
1.0
in theWeight X
layout constraint value field and press Enter. -
Select the text field to the right of the
City
label in the Grid Area of the GridBag Customizer. -
Select
Horizontal
in theFill
combobox and press Enter. -
Type
1.0
in theWeight X
layout constraint value field and press Enter.
To verify that the designed container resizes correctly in horizontal direction, click the Test Layout button () in the Customizer’s toolbar and drag the borders of the ContactsBasicInitial
form.
Anchoring
Anchoring is used when the component is smaller than its display area to determine where (within the area) to place the component.
During horizontal resizing of the ContactsBasicInitial
form in the previous section you have probably noticed that the State
combobox moves away from the State
label. Since the preferred size of the combobox is smaller than the size of the corresponding cell, the GridBagLayout places the component into the center of the cell by default.
To change this behaviour, specify the Anchor
layout constraint as follows:
-
Select the combo-box to the right of the
State
label and click the arrow button () to the right of theAnchor
combobox in the Property Sheet of the Customizer. -
Choose
Line Start
from the drop-down list.
The State
combo-box is anchored to the left side of the form when the latter is resized horizontally now.
To get the labels aligned to the left instead of to the center as they are at the moment, complete the steps below:
-
Select the
First name:
,Surname:
,Street
,City
, andState
labels.
You can select multiple components by pressing the left mouse button on the first component, holding it, and dragging it pressed to the last component as if drawing a rectangle that encloses all the labels. After you release the mouse all the five components are highlighted with orange borders and green background as shown below. |
-
Change the
Anchor
layout constraint of the labels toLine Start
. The labels are anchored to the left.
Spacing
By default, each component has no external padding. The Inset
constraint specifies the external padding of the component - the minimum amount of space between the component and the edges of its display area.
In the current layout, the components are placed too close to each other. To separate them, do the following:
-
Ctrl-click to select all the components.
-
Press the button to the right of the Insets constraint text field.
-
In the displayed dialog box, change
Top:
andLeft:
values to5
and click OK.
Your form should look now like the one from the ContactsBasicFinal.java
file if you open it.
Summary
In this short tutorial, you designed a simple form. When editing the layout you learned how to use the basic features of the GridBag Customizer.
You can now go to the second in a two-part series tutorial where you will modify the ContactsAdvancedInitial
form to get familiar with the GridBag Customizer advanced features.
Summary
You have now completed the Designing a Basic Java Form Using the GridBag Customizer tutorial. For information on adding functionality to the GUIs that you create, see: