Gap Editing Support in the NetBeans GUI Builder
A layout of a container in the Free Design mode consists of components and gaps between these components. Both the components and gaps are visualized in the Design view of the GUI Builder. The NetBeans IDE enables you to edit gaps directly in the GUI Builder.
This tutorial demonstrates how to utilize gap editing to insert new UI components between other components as well as how to center components easily around a frame in the NetBeans GUI Builder without concern for the underlying layout manager. The tutorial is intended as a guide to show how you can perform changes in an existing form in the Free Design mode to implement a specific target layout that is required by the project.
To follow this tutorial, you need the software and resources listed below.
|Software or Resource||Version Required|
An archive with the source files containing the initial and target tutorial layouts.
You can download the project that is used as the starting point for this tutorial as a
This tutorial focuses on designing the layout of the container only. Adding functionality to the GUI is out of its scope.
You can turn on and off visualization of the gaps by using the
Visualize Additional Layout Informationoption after choosing
GUI Builderin the main IDE’s menu.
Download and unzip the GapSupport.zip archive to any location on your computer.
In the NetBeans IDE main menu, choose
Open Project, navigate to the folder that contains the unzipped files with the
GapSupportproject that you extracted in the previous step.
Click Open Project. The Projects window should look like the following:
Initial.javafile. The sample form opens in the GUI Builder Design view.
|You can view the component hierarchy of the form in the Navigator window by choosing Window > Navigator from the main toolbar.|
Let us explore how to edit a gap by dragging and dropping its edge in the Design view of the IDE.
To add a
Middle Name row between the
First Name and
Last Name rows, you need to complete the following steps:
Click on the gap between the
Last Namelabels. The gap is highlighted with green.
Hover the mouse pointer over the bottom part of the highlighted gap. The pointer is changed to a vertical resizable one.
Enlarge the selected gap to 50 by pressing the left mouse button, dragging the pointer downward, and releasing the left mouse button. The new size of the gap is displayed in a tooltip.
Add a new label into the created gap by dragging it from the Swing Controls section of the Palette and dropping it so that its left edge is aligned with the left edge of the
First Namelabel and its top edge has the suggested preferred gap from the
Double-click the label and change the text of the label to
Add a new text field to the right of the
Middle Name:label by dragging it from the Swing Controls section of the Palette and dropping it so that it is baseline-aligned with the
Middle Namelabel and its left edge is aligned with the other text fields.
Drag the right edge of the text field to align it with the right edge of the other text fields.
Right-click the text inside the text field and choose Edit Text from the popup menu. Remove the text.
Middle Name row is inserted between the form components.
The IDE enables you to resize a gap by clicking and then scrolling a mouse wheel to tune the gap size.
To remove the remaining space between the
Middle Name and
Last Name rows, click the gap below and decrease the height of the gap by scrolling the mouse wheel downward and setting the new size to
default small .
The NetBeans GUI Builder supports three preferred gaps for component placement -
The gap between the form components is resized by using the mouse wheel and utilizing a preferred gap.
You can center a component by enclosing it into two identical gaps that have prior been marked as resizable.
|A container helps specify where the components should be centered. It is possible to center the buttons without enclosing them in a new panel but it is more difficult to accomplish in the GUI Builder and the resulting layout is a bit fragile. Therefore, we suggest to enclose the component being centered in a panel whenever it is possible.|
To enclose the buttons and resizable gaps into a separate container, do as follows:
Select all the four buttons in the form.
Right-click the selection and choose
Panelfrom the popup menu.
The buttons are enclosed into a container.
To remove the newly created gaps on the left and right side of the buttons, complete the following steps:
Right-click one of the buttons and choose Edit Layout Space from the popup menu. The Edit Layout Space dialog box is displayed.
Set the size of the Left and Right gaps to 0 and click OK. The gaps to the left and right of the buttons are removed using the Edit Layout Space dialog box.
To make the gaps above and below the container resizable, do as follows:
Double-click the gap at the bottom of the last button. The Edit Layout Space dialog box is displayed.
In the Edit Layout Space dialog box, select the
Resizableoption and click OK.
Repeat steps 1 and 2 for the gap above the topmost button. The gaps above and below the container with the buttons are made resizable.
To center the buttons of the sample form:
Drag the bottom edge of the container with the buttons to align with the bottom edges of the lists as shown below:
The container is stretched to match the height of the
Available Topics and
Selected Topics lists. The buttons are centered within the space determined by the enclosing container since the surrounding gaps have been marked as resizable.
In this tutorial you enhanced a simple form. When manipulating gaps you learned how to manage empty spaces in the Free Design mode and design an appealing UI without spending extra time on tweaking every detail of the layout implementation.