Using Hibernate

in a Java Swing Application 5

 

 

  1. Creating the Application GUI

    1. Creating the JFrame Form

    2. Adding Elements to the Form

 

Creating the Application GUI

 

In this exercise you will create a simple JFrame form with some fields for entering and displaying data. You will also add a button that will trigger a database query to retrieve the data.

 

Creating the JFrame Form

 

Select and right-click the project node in the Projects window and choose New > Other to open the New File wizard.

 

 

Invoking the New File Wizard from the Project pane for Java Hibernate project

 

Select JFrame Form from the Swing GUI Forms category. Click Next.

 

Selecting the JFrame Form file type from Java Swing GUI Forms

 

Type MyDVDStoreAdmin for the Class Name and type sakila.ui for the Package. Click Finish.

 

Choosing the class and package name for JFrame form

 

When you click Finish the IDE create the class and opens the JFrame Form in the Design view of the editor.

 

The JFrame swing Java element seen in the Design view

 

Adding Elements to the Form

 

You now need to add the user interface (UI) elements to the form. When the form is open in Design view in the editor, the Palette appears in the left side of the IDE. To add an element to the form, drag the element from the Palette into the form area. After you add an element to the form you need to modify the default value of the Variable Name property for that element.

 

Drag a Label element from the Palette and change the text to Actor Profile.

 

Adding the Java Label element to the JFrame container in NetBeans 6.9.1

 

 

Changing the Swing label default text to Actor Profile

 

Drag a Label element from the Palette and change the text to First Name.

 

Adding another Swing Label element to the JFrame container

 

Drag a Text Field element next to the First Name label and delete the default text.

 

Adding the text field element to the JFrame container in NetBeans 6.9.1

 

-------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------

 

Deleting the default text of the text field

 

Drag a Label element from the Palette and change the text to Last Name.

Then, drag a Text Field element next to the Last Name label and delete the default text.

Drag a Button element from the Palette and change the text to Query.

 

Adding a button the the JFrame container

 

Changing the Swing button default text to Query

 

Drag a Table element from the Palette into the form.

 

Adding a Swing Table element to the JFrame container

 

Modify the Variable Name values of the following UI elements according to the values in the following table.

 

Element

Variable Name

First Name text field

firstNameTextField

Last Name text field

lastNameTextField

Query button

queryButton

Table

resultTable

 

You can modify the Variable Name value of an element by right-clicking the element in the Design view and then choosing Change Variable Name. Alternatively, you can change the Variable Name directly in the Inspector window.

You do not need to assign Variable Name values to the Label elements.

 

Changing the Swing text field varaible name

 

Assigning a new variable name to the Java Swing text field element

 

Changing the variable name of the Swing text field element

 

Assigning new variable name to another Swing text field element

 

Invoking the Change Variable Name context menu from the Java Swing Design view

 

Assigning new variable name to the Java Swing button element

 

The following screenshot shows how to change the variable name from the Inspector window of NetBeans 6.9.1. Just select by clicking the element and change the variable name to a new one directly.

 

Changing variable name from the Inspector window in the NetBeans 6.9.1 IDE

 

You can test preview the GUI design by clicking the following icon.

 

Previewing the Swing elements design in NetBeans 6.9.1 IDE

 

In Design view your form should look similar to the following Figure.

 

A completed design form using Java Swing elements seen in NetBEans 6.9.1

 

Save your changes. Now that you have a form you need to create the code to assign events to the form elements. In the next exercise you will construct queries based on Hibernate Query Language (HQL) to retrieve data. After you construct the queries you will add methods to the form to invoke the appropriate query when the Query button is pressed.

 

Build the application. Select the project root folder > right-click mouse > select Build menu.

 

Invoking the Build context menu from the Project window in NetBeans 6.9.1

 

Make sure there is no error that can be seen in the Output window.

 

The Build sample output log seen in the Output window

 

 

 

 

 

 

 


< Java Swing and Hibernate 4 | Java and NetBeans Tutorial | Java Swing and Hibernate 6 >