Way2Java

TextField Applet Adding Two Numbers

TextField Applet - TwoNumbers.java

After validating user name and password using TextField in an application (application extends frame), let us rewrite the same program using applets but with a different functionality. We take two numbers from the user and display the sum of two numbers.

The following program comes with 3 text fields. In two text fields, user enters two numbers. In the third text field, the programmer displays their sum.

Example on TextField Applet Adding Two Numbers

Applet Program. File Name: TwoNumbers.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TwoNumbers extends Applet implements ActionListener
{
   TextField firstNum, secondNum, resultNum;
   public TwoNumbers()
   {                      
      setLayout(new GridLayout(3, 2, 10, 15));  
      setBackground(Color.cyan);

      firstNum = new TextField(15);
      secondNum = new TextField(15);
      resultNum = new TextField(15);

      secondNum.addActionListener(this);

      add(new Label("Enter First Number"));  add(firstNum);
      add(new Label("Enter Second Number")); add(secondNum);
      add(new Label("S U M"));               add(resultNum);
   }
   public void actionPerformed(ActionEvent e)
   {                        
      String str1 = firstNum.getText();
      double fn = Double.parseDouble(str1);
      double sn = Double.parseDouble(secondNum.getText());

      resultNum.setText("Sum is " + (fn+sn));
   }
}

HTMLfile to run the above applet. File Name: TN.html


public class TwoNumbers extends Applet implements ActionListener

Observe, the class TwoNumbers extends Applet (not Frame) and as usual implements ActionListener for event handling.

setLayout(new GridLayout(3, 2, 10, 15));
setBackground(Color.cyan);

The applet is set with GridLayout of 3 rows, 2 columns, 10 horizontal and 15 vertical gaps. For an attractive display purpose, a background of predefined color cyan is set.

Three text fields are created, firstNum, secondNum and resultNum. The secondNum (where the user enters the second number) is given the action (with addActionListener()). Whenever, the user presses enter key, the event handling takes place and the sum of two number is displayed in the resultNum field.

add(new Label(“Enter First Number”));

For display purpose of what for the text filed is meant, three anonymous objects of Label component are created and added. Using anonymous objects saves memory on the client machine. The above statement would have been like this also.

Label lab1 = new Label(“Enter First Number”);
add(lab1);

In the above statement, a memory of 4 bytes for Label object lab1 is wasted.

String str1 = firstNum.getText();
double fn = Double.parseDouble(str1);

The getText() method of TextField returns always a string of what user enters in the text filed whether it may be a name, password or a number. For our calculation purpose, the number in the form of string is converted to a double value. This is done with parsing operation using parseDouble() method.

double sn = Double.parseDouble(secondNum.getText());

In the earlier statement, string str1 is used as temporary variable to carry the data to the parseDouble() method. Even this variable can be avoided by passing direct getText() method to the parseDouble() method. In realtime, practice, the programmer does like this only and avoids number of lines of code and also improves the performance.

resultNum.setText(“Sum is ” + (fn+sn));

With setText() method, the value is displayed in the resultNum field. Remember, the setText() method requires a string as parameter. fn+sn when concanated to “Sum is ” becomes a string. Any stirng concatenated with a data type or any other value, results in a string. The following statement raises compilation error.

resultNum.setText(fn+sn);

Then how to do display only fn+sn in the text field. Convert it into a string, using valueOf() method of String class and then pass as follows.

String str = String.valueOf(fn+sn);
resultNum.setText(str);

Alternatively, in a single statement, it could done as follows avoiding the string str.

resultNum.setText(String.valueOf(fn+sn));

For other explanation of the program, refer to the application Java TextField – User Name Password Validation.

The migration steps from application to applet are available at Java AWT Radio Buttons – Applet GUI.