NetBeans GUI Builder

The IDE's GUI Builder has been revamped to make it more powerful and intuitive, liberating users to build professional looking GUIs without an intimate understanding of layout managers. NetBeans' GUI Builder eliminates the difficulties of building GUIs, enabling you to lay out your forms by simply placing components where you want them.

This tutorial is geared to the beginner and introduces the basic construction of a GUI. A basic understanding of the Java Programming Language is expected.






Exercises


Exercise 1: Create ContactEditor GUI using NetBeans GUI Builder

This exercise guides you through the process of creating the graphical user interface (GUI) for an application called ContactEditor using the NetBeans IDE's GUI Builder (also known by the code name Matisse). In the process, you will layout a GUI front-end that enables you to view and edit contact information of individuals included in an employee database.

This exercise is based on the GUI Building in NetBeans IDE article written by Talley Mulligan from www.netbeans.org.

  1. Create a new NetBeans project
  2. Add a Panel to the project
  3. Add components to the Panel
  4. Insert a component
  5. Preview the design work
  6. Fix abnormalities
  7. Add a Combo Box component
  8. Observe the source code generated

(1.1) Create a new NetBeans project

0. Start NetBeans IDE. 
1. Create a NetBeans project


Figure-1.11: Create a new NetBeans project


Figure-1.12: Create Java Desktop Application project

You might see the following disclaimer depending on the version of NetBeans IDE version (for example, NetBeans 6.9). Click Next
if you do.


2.  Give a name to the project.


Figure-1.13: Name and Location pane

                                                                                                              return to top of this exercise

(1.2)  Add a Panel to the project

1. Add a Panel component to the Design window.  (In this document, the terms "Panel" and "JPanel" are used interchangeably.)


Figure-1.21: Add a JPanel container component to the GUI

2. Resize the Panel component.



Figure-1.22: Extend the JPanel  to the right

3. Change the properties of the JPanel component to add a border.


Figure-1.23: Change the border property of the JPanel container









Figure-1.24: Set title, color, font for the Panel component

                                                                                                              return to top of this exercise

(1.3)  Add components to the Panel


Now we need to start adding the components that will present the actual contact information in our contact list. In this step, we'll add four JTextFields that will display the contact information and the JLabels that will describe them. While accomplishing this, notice the horizontal and vertical guidelines that the GUI Builder displays, suggesting the preferred component spacing as defined by your operating system's look and feel. This ensures that your GUI is automatically rendered respecting the target operating system's look and feel at runtime.

1. Add a Label and Text Field components to the JPanel container component.  (In this document, the terms "Label" and "JLabel" are used interchangeably.)

Figure-1.31: Add a Label component to the JPanel container

Figure-1.32: Add Label and Text Field components to the JPanel container

2. Add another set of Label and Text Field components.  (In this document, the terms "Text Field" and "JTextField" are used interchaneably.)


Figure-1.33: Add Last Name Label and Text Field components

3. Add two Label Components.

Now we'll add the Title: and Nickname: JLabels that describe two JTextFields that we're going to add in a minute. We'll pick and plop the components while pressing the Shift key, to quickly add them to the form. While accomplishing this, again notice that the GUI Builder displays horizontal and vertical guidelines suggesting the preferred component spacing.

Figure-1.34: Add Title: and Nickname: Label components

                                                                                                              return to top of this exercise


(1.4) Insert components


Often it is necessary to add a component between components that are already placed in a form. Whenever you add a component between two existing components, the GUI Builder automatically shifts them to make room for the new component. To demonstrate this, we'll insert a JTextField component between the Title: and Nickname: components we added previously.

1. Insert a JTextField component between two JLabel components

Figure-1.41: Insert a Text Field component between the two Label component

Figure-1.42: Text Field component is now inserted between the two Label components

2. Add a Text Field component.

Figure-1.43: New Text Field component is added

                                                                                                              return to top of this exercise


(1.5) Preview the design work


You can do the Preview to see what you've done so far.

1. Click the Preview Design button.  (Figure-1.51 below)


Figure-1.51: Preview Design

2. Observe abnormalities. 

Figure-1.52: Preview

3. Observe the resizing abnormality.

Figure-1.53: Resizing abnormality

In this step, you have observed three abnormalities.  In the next step, you are going to address them.

                                                                                                              return to top of this exercise


(1.6) Fix the abnormalities


1. Right align the Labels.

Figure-1.61: Right align the First Name: and Title: JLabel components

Figure-1.62: Right alignment is performed

2. Fix resizing problem.

Figure-1.63: Change horizontal resizability



3. Make all Text Fields to be the same size.

Figure-1.64: Set Default size

4. Set resizing for all Text Field components.


5. Preview Design



                                                                                                              return to top of this exercise


(1.7) Add a Combo Box component


1. Add a new Label component. (Figure-1.71 below)

Figure-1.71: Add a new label

2. Add a Combo Box component.


Figure-1.72: Combo Box is added

Figure-1.73: Resize the height of the JPanel container

                                                                                                              return to top of this exercise

(1.8) Observe the source code generated


1. Click Source tab window and observe the source code that was generated by the GUI Builder. (Figure-1.81 below)


Figure-1.81: Source code that was generated by the GUI Builder

                                                                                                              return to top of this exercise

Summary

In this exercise,  you have created a ContactEditor GUI form by using NetBeans GUI builder.  You learned how to drag the GUI components and position them in the right place with the help from the GUI builder. You learned how to resize and realign components.

                                                                                                              return to the top




Exercise 2: Add Event Handlers to GUI Components

This exercises shows you how to build a simple GUI with event handler functionality. 

This exercise is based on the Adding Functionality to Buttons: A Beginners Guide article written by Saleem Gul and Tomas Pavek, maintained by Ruth Kusterer from www.netbeans.org.

    1. Create a NetBeans project
    2. Add JPanel container
    3. Add components to the JPanel container
    4. Add event handlers to the components

(2.1) Create a NetBeans project

1. Create a NetBeans project


Figure-2.11: Create NumberAddition project

                                                                                                              return to top of this exercise

(2.2) Add JPanel container

1. Add JPanel component to the Designer window.

2. Resize the JPanel component.

3. Change the properties of the JPanel component.


Figure-2.21: Change properties of the JPanel

                                                                                                              return to top of this exercise


(2.3) Add components


1. Add various component as shown in Figure-2.31 below.  You are adding three Label components and three Text Field components and three Button components.



Figure-2.31: Add components to the JPanel

2. Change the text display values of the Label components  to First Number:, Second Number:, and Result: as shown in Figure-2.32 below. 
3. Right align the three Label components.


Figure-2.32: Change text values of the Labels

4. Right click jTextField1 Text Field component and select Edit Text. (Figure-2.33 below)


Figure-2.33: Select Edit Text

5. Remove jTextField1 string.
6. Repeat the process for the other two Text Field components.
7. Right click jButton1 component and select Edit Text and charge the value to Add. (Figure-2.34 below)
8. Repeat the process for the other two Button components and change the value to Clear and Exit.


Figure-2.34: Change the text values of the components

9. Observe the final result looks as shown in Figure-2.35 below.


Figure-2.35: Number Addition GUI

                                                                                                              return to top of this exercise

(2.4) Add an event handler to GUI components


In this step, you are going to register event handlers to the Buttons.  When a button is pressed, the event handler that is registered for that button will be invoked.

1. Right click Exit button and select Events->Action->actionPerformed.


Figure-2.41: Add event handler to Exit button

2. Modify the event handler for the Exit button as shown in Figure-2.42 below. When Exit button is pressed, the event handler called jButton3ActionPerformed gets invoked. The code fragment that needs to be added is highlighted in bold-font. 

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);
}


Figure-2.42: System.exit(0) method is added to the event handler

3. Right click Clear button and select Events->Action->actionPerformed. (Figure-2.43 below)


Figure-2.43: Add event handler

4. Modify the event handler as shown in Code-2.45 and Figure-2.46 below. The code fragment that needs be added is high-lighted in bold-font.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
}
Code-2.45: Modified event handler


Figure-2.46: Modified event handler for button 2

5. Right click Add button and select Events->Action->actionPerformed. (Figure-2.47 below)


Figure-2.47: Add event handler

6. Modify the event handler as shown in Code-2.48 and Figure-2.49. The code fragment that needs be added is high-lighted in bold-font.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
    // First we define float variables.
    float num1, num2, result;
    // We have to parse the text to a type float.
    num1 = Float.parseFloat(jTextField1.getText());
    num2 = Float.parseFloat(jTextField2.getText());
    // Now we can perform the addition.
    result = num1+num2;
    // We will now pass the value of result to jTextField3.
    // At the same time, we are going to
    // change the value of result from a float to a string.
    jTextField3.setText(String.valueOf(result));
}
Code-2.48: Modified event handler


Figure-2.49 Modified event handler
                                                                                                              return to top of this exercise

(2.5) Build and run the application


1. Right click the NumberAddition project and select Run. (Figure-2.51 below)


Figure-2.51: Run the application

2. Perform Add operation.

Figure-2.52: Perform Add operation

Figure-2.53: The result is displayed

3. Perform Clear operation.

Figure-2.54: Perform Clear operation

4. Perform Exit operation.

Figure-2.55: Perform Exit operation

                                                                                                              return to top of this exercise


Homework


1. The homework is to modify the NumberAddition project to add Subtract button.  The Subtract button subtracts the value of the Second Number field from the value of the First Number field and displays the result in the Result field.  (You might want to create a new project by copying the MyNumberComputer project.  You can name the homework project in any way you want but here I am going to call it MyNumberComputer.)