Introduction

This article shows how to create a GUI, in other words, a Graphical User Interface and add a few back-ends functionalities using the NetBeans IDE. You will be able to work with jLabel, jTextField, jComboBox and jButton easily using NetBeans.
Initial Stage: Creating a project
Use the following procedure to create the project:
  1. Select "File" -> "New Project" from the main menu bar.
  2. Select Java in categories and "Java Application" in the project. Click the "Next" button.
  3. Write WelcomeApp in the Project Name and specify the path.
  4. You can choose "Use Dedicated Folder for Storing Libraries" to specify the location for the library folder. This step is optional.
  5. Deselect the "Create Main Class" CheckBox if selected. Finally, click "Finish". Now your project's initial stage of project creation is complete. Now we will build the Front-End.
Creating the Front-End
  1. In the project window you will see a node with your project name, WelcomeApp; right-click on this node and select "New" -> "Other".
  2. A dialog box with the name New File will appear, select Swing GUI Forms in categories and jFrame Form file type then click "Next".
  3. Type WelcomeAppUI as class name and my.welcomeapp as the package.
  4. Finally click the "Finish" button.

    After following the preceding steps the NetBeans IDE creates the WelcomeAppUI form and also the WelcomeAppUI class on the WelcomeApp application and finally opens a WelcomeAppUI form in the GUI builder. Our my.WelcomeApp package replaces the default package.
14.1.jpg
  1. Pick the jPanel from the Swing Container in the Palette and drop it onto the jFrame.
  2. Select the border of the jPanel by clicking on the ellipsis (...) button next to Border.
  3. In the border dialog box, select "Titled Border" from the list and write "Welcome App" for the title TextField provided. And proceed according to the preceding screenshot and design the application.
  1. Double-click on the JLabel1 and rename it to First Name.
  2. Double-click on the JLabel2 and rename it to Last Name.
  3. Double-click on the JLabel3 and rename it to Interest.
  4. Right-click on the jTextFields and select "Edit Text" from the pop-up menu and delete the default text.
  5. Rename the JButton1, JButton2 and JButton3 to "Submit", "Clear" and "Exit" respectively by right-clicking on each button and selecting "Edit Text" from the pop-up menu.

    Finally the Application front-end will look as in the following screenshot.
14.2.jpg
  1. Functionalities of the "Exit" button:
The default code that will appear is as follows:
  1. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
  2. {
  3. // TODO add your handling code here:
  4. }
Now we need to edit the code. Replace the comment with System.exit(0); and the code is as follows:
  1. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
  2. {
  3. System.exit(0);
  4. }
  1. Functionalities for the "Clear" button.
  1. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
  2. {
  3. jTextField1.setText("");
  4. jTextField2.setText("");
  5. jTextField3.setText("");
  6. jTextField4.setText("");
  7. }
The code above will clear all the four jTextFields.
  1. Functionalities of the "Submit" button.

    The "Submit" button will pick the data inserted by the user and work according to the following statements:
  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
  2. {
  3. String nm=jTextField1.getText();
  4. String nb=jTextField2.getText();
  5. jTextField4.setText("WelcomApp Welcomes you "+nm+" "+nb);
  6. jTextField3.setText("You like "+Interest.getSelectedItem());
  7. }
Now our code for the WelcomeApp is complete.
14.3.jpg
After clicking on the Submit Button:
14.4.jpg
Click the "Exit" button to Quit.