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.

Contents

Content on this page applies to NetBeans IDE 7.2, 7.3, 7.4 and 8.0

To follow this tutorial, you need the software and resources listed below.

Software or Resource Version Required
NetBeans IDE 7.2, 7.3, 7.4, or 8.0
Java Development Kit (JDK) Version 6, 7, or 8
GapSupport.zip An archive with the source files containing the initial and target tutorial layouts.

Notes:

  • You can download the project that is used as the starting point for this tutorial 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.
  • You can turn on and off visualization of the gaps by using the Visualize Additional Layout Information option after choosing Tools > Options > Java > GUI Builder in the main IDE's menu.

Opening Example Project

  1. Download and unzip the GapSupport.zip archive to any location on your computer.
  2. In the NetBeans IDE main menu, choose File > Open Project, navigate to the folder that contains the unzipped files with the GapSupport project that you extracted in the previous step.
  3. Click Open Project.
    The Projects window should look like the following:

    The Projects window

  4. Double-click the Initial.java file.
    The sample form opens in the GUI Builder Design view.

    Initial.java open

    Note: You can view the component hierarchy of the form in the Navigator window by choosing Window > Navigator from the main toolbar.

    Navigator window

top

Resizing a Gap by Dragging and Dropping Its Edge

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:

  1. Click on the gap between the First Name and Last Name labels.
    The gap is highlighted with green.

    Gap highlighted

  2. Hover the mouse pointer over the bottom part of the highlighted gap.
    The pointer is changed to a vertical resizable one.

    Pointer changed to resizable

  3. 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.

    Enlarging the gap

  4. 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 Name label and its top edge has the suggested preferred gap from the First Name row.

    Adding a label

  5. Double-click the label and change the text of the label to Middle Name:.

    Text changed to Middle Name:

  6. 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 Name label and its left edge is aligned with the other text fields.

    Adding a text field

  7. Drag the right edge of the text field to align it with the right edge of the other text fields.

    Resizing the text field

  8. Right-click the text inside the text field and choose Edit Text from the popup menu. Remove the text.

The Middle Name row is inserted between the form components.

Resizing the text field

top

Resizing a Gap Using the Mouse Wheel

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.

Note: The NetBeans GUI Builder supports three preferred gaps for component placement - default small, default medium, and default large.

default small

The gap between the form components is resized by using the mouse wheel and utilizing a preferred gap.

Middle Name row inserted

top

Editing Gaps Around a Component

You can center a component by enclosing it into two identical gaps that have prior been marked as resizable.

Note: 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:

  1. Select all the four buttons in the form.
  2. Right-click the selection and choose Enclose In > Panel from the popup menu.

    Choosing Panel in the popup menu

    The buttons are enclosed into a container.

    Buttons enclosed into a panel

To remove the newly created gaps on the left and right side of the buttons, complete the following steps:

  1. Right-click one of the buttons and choose Edit Layout Space from the popup menu.
    The Edit Layout Space dialog box is displayed.

    Edit Layout Space dialog box

  2. 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.

    Left and Right Gaps Removed

To make the gaps above and below the container resizable, do as follows:

  1. Double-click the gap at the bottom of the last button.
    The Edit Layout Space dialog box is displayed.
  2. In the Edit Layout Space dialog box, select the Resizable option and click OK.

    Gap at the bottom being resized

  3. 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:

Centering the container with the buttons

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.

Buttons are centered

top

Summary

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.

top


See Also

You have now completed the Gap Editing Support in the NetBeans GUI Builder tutorial. For information on adding functionality to the GUIs that you create, see:

top

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo