Advantages of NetBeans IDE

Introduction

This article explains the advantages of the NetBeans IDE. A "Celsius To Fahrenheit" application is used as a sample.

First we create this app simply in a text editor like Notepad++.

Creating a CelToFarConverter GUI

We need to use the following procedure to create this app.

Step 1

We need to import several packages like:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

Swing is used to design the layout, in other words create a user interface and AWT is used to handle button event.

Step 2

Define a class to extend JFrame and that implements an ActionListener. Now we create one button object, two label components and two text fields as shown below.

public class CelToFarConverter extends JFrame implements ActionListener

  {

    JButton btn;

    JLabel lb1, lb2;

    JTextField tf1,tf2;

Step 3

Now create a constructor to display the button, label and text field and set their property as shown below.

Public CelToFarConverter()

      {

        add(lb1=new JLabel("Celsius"));

        add(tf1=new JTextField(20));

        add(lb2=new JLabel("Fahrenheit"));

        add(tf2=new JTextField(20));

        add(btn=new JButton("Click To Convert"));

        setVisible(true);

        tf2.setEditable(false);

        setSize(600,300);

        setTitle("Celsius To Fahrenheit Converter");

        setLayout(new FlowLayout());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(this);

      }

Step 4

Now create a method of ActionListener to generate an action in which it gets text from the user input and then converts it using a formula and displays it in another textfield as shown below.

public void actionPerformed(ActionEvent e)

      {

        float tempFahr = (float)((Double.parseDouble(tf1.getText()))

            * 1.8 + 32);

                     tf2.setText(tempFahr + " F");

      }

Step 5

Create a main method to start executing our app. As we know the compiler starts exection through main so create an object of our class to start construtor exection. As shown below.

public static void main(String args[])

      {

        new CelToFarConverter();

      }

Complete Coding

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class CelToFarConverter extends JFrame implements ActionListener

  {

    JButton btn;

    JLabel lb1, lb2;

    JTextField tf1,tf2;

    Public CelToFarConverter()

      {

        add(lb1=new JLabel("Celsius"));

        add(tf1=new JTextField(20));

        add(lb2=new JLabel("Fahrenheit"));

        add(tf2=new JTextField(20));

        add(btn=new JButton("Click To Convert"));

        setVisible(true);

        tf2.setEditable(false);

        setSize(600,300);

        setTitle("Celsius To Fahrenheit Converter");

        setLayout(new FlowLayout());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(this);

      }

    public void actionPerformed(ActionEvent e)

      {

        float tempFahr = (float)((Double.parseDouble(tf1.getText()))

            * 1.8 + 32);

                     tf2.setText(tempFahr + " F");

      }

    public static void main(String args[])

      {

        new CelToFarConverter();

      }

  }

Output

Compile the program.

Fig-1.jpg

Press Enter to see the application.

Fig-2.jpg

Now we provide input in Celsius.

Fig-3.jpg

Now click on the button ("Click To Convert") and the results are shown below.

Fig-4.jpg

Using the NetBeans IDE

We will now create this app using the NetBeans IDE.

We create the same app in the NetBeans IDE. The purpose of this is to understand the advantages of using the NetBeans IDE. The NetBeans IDE provides drag and drop capability to relieve us of the necessity to write code to display a button, textfield, label, and so on. Let's proceed to create this app.

Step 1

Open the NetBeans IDE then click on the "File" Menu then choose "New Project". A window is generated similar to the following figure.

Fig-5.jpg

Step 2

Now choose "Java" -> "Java Application" -> click on "Next". Provide your project name. I used CelciusToFahrenheitCoverter as shown below.

Fig-6.jpg

Step 3

Now click on "Finish". Your project is generated; now right-click on the project and choose "New" -> "JFrame Form" as shown below.

Fig-7.jpg

Step 4

Now provide your class name. I used "CelToFarConverterGUI" as shown below.

Fig-8.jpg

Step 5

Now click on "Finish". A JFrame is added by the NetBeans IDE as shown below.

Fig-9.jpg

Step 6

Now you see the advantages of using the NetBeans IDE. In this application we did not need to write code to display various components. NetBeans provides Drag and Drop capability to add several components of a frame.

Now first we change the title of our frame.

In the Navigator window Right-click on "JFrame" then choose "Properties" as shown below.

Fig-10.jpg

Step 7

You see the "title" property in the properties window. In the right corner you see there is an icon bar; click on that icon. A new window is generated. Type your Frame title there as shown below.

Fig-11.jpg

Step 8

Click on "OK" -> "Close" to close the properties window. Now the title will be changed. Now start a Drag and Drop in the frame.

Open the Palette by pressing ("CTRL+SHIFT+8"). The Palette window is shown below.

fig-12.jpg

Step 9

Start drag and drop; we need to drag one button, two labels, and two text fields. First add a label then textfield. Similarly, the next label and a textfield and finally add a button. For drag you need to click on the label and then click on the frame where you need to add this as shown below.

fig-13.jpg

Similarly add another field and now your frame is similar to the following frame.

fig-14.jpg

Step 10

Change the default content written inside it. Right-click on them one by one. First right-click on "Label1" and choose "Edit Text" as shown below.

fig-15.jpg

Step 11

Now change the name of each component according to your use. We use the following name.

Fig-16.jpg

Step 12

Now resize the component and then resize the frame. For resizing, click on each component separately and resize it. Now the frame is as shown below.

Fig-17.jpg

You will be surprised if you see its coding by clicking on "Source Part", that's code generated by the NetBeans IDE automatically. Take a look at the following.

Fig-18.jpg

Note: You don't need to make changes in the code so leave it as is.

Step 13

Now go to the "Navigator Window" and change the name of the component.

Right-click on each component and choose "Change Variable Name" and provide the following name.

fig-20.jpg

Step 14

Now add ActionListener to the button to generate the result.

Right-click on the button then choose "Event" -> "Action" -> "Action Performed" as shown below.

Fig-19.jpg

Step 15

Write the following code in the actionPerformed methd.

float tempFahr = (float)((Double.parseDouble(tf1.getText()))
* 1.8 + 32);
tf2.setText(tempFahr + " F");

Fig-21.jpg

Now your project is ready to run.

Step 16

Right-click on the project and choose "Run". The following output is generated.

Fig-22.jpg

Celsius column

Now we need to provide only one input for the Celsius column only.

Provide a nuber and click on the button; you will see the following output:

Fig-23.jpg

Click on the button.

fig-24.jpg

Summary

The main purpose of this article is to illustrate the advantages of Netbeans. As you can see, we need to write code only for ActionListener. It's so easy to develop a GUI application in the NetBeans IDE. Thanks for reading.