# Java Lecture Chapter 1

#### Marvic

##### Member
Using Command Button and Text Field
The command button control is used to begin, interrupt or end a process. When clicked, it invokes a command that has been written into its Click event procedure. Most Java applications have command buttons that allow the user to simply click them to perform actions. When the user clicks the button, it is not only carries out the appropriate action, it looks also like as if it’s being pushed in and released and is therefore sometimes referred to as push button.

The text field control is used to display information entered by the user or assigned to the Text property of the control at design time. Without further ado, let us now have our first example.
Before jumping right away with command button and text field program development, let us try to run the example above which simply display a message in our applet.

Example 1:
Design and develop a Java program that will display the message, “Hello Bianca!” on the applet. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.Graphics;
public class hello1 extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Bianca!",60,30);
}
}

2. Then save the Java program with the filename: hello1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>hello1</title>
<body>
<hr>
<applet
code=hello1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: hello1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac hello1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\hello1.htm

Explanation:
Code:
import java.awt.Graphics;
public class hello1 extends java.applet.Applet
{

The import java.awt.Graphics means that we want to include the Java Graphics class and make use of it in our program. Furthermore, we have added support for graphics-handling by including the java.awt.Graphics class. In Java programming language, displaying the text string “Hello Bianca!” is considered a graphics handling.

Next, it is time to set up our hello1 applet itself. To accomplish it, we have to define a new class named hello1. This is the standard way of setting up applet in Java programming. The Java applet has the filename extension .class. It is because each class defined in a .java file ends up being exported to a .class file (after compilation), where we can make use of it.

We can customize the java.applet.Applet class by deriving the hello1 class from the java.applet.Applet class. This makes java.applet.Applet the base class of the hello1 class, and it makes hello1 a class derived from java.applet.Applet. This gives us all the power of the Java.applet.Applet .

This technique is called inheritance in Object-Oriented Programming (OOP) paradigm. In inheritance, a derived class inherits the functionality of its base class and adds more on top of it. For example, we may have a base class called, say, vehicle. We can derive various classes from this base class called, say, bus, motorcycle, or truck. In this way, three derived classes can share the same base class, saving time and effort programmatically.

Although the bus, motorcycle, and truck classes share the same base class, vehicle, they added different items to the base class, ending up as three quite different classes, bus, motorcycle, and truck. In using inheritance, we can extend the base class java.applet.Applet by creating our own class hello1 and adding onto the base class.

The keyword class indicates that we are defining a new class in our code.
Code:
  public void paint(Graphics g)
{
g.drawString("Hello Bianca!",60,30);
}
} (x,y)

One of the functions in the java.applet.Applet class is paint( ). This function is called when the Web browser tells the applet to create its display on the screen. This happens when the applet first begins and every time it has to be re-displayed later. Like for example, if the Web browser was minimized and then maximized, or if some window was moved and the applet’s display area was uncovered after having been covered.

Our goal in the hello1 class is to display the text string “Hello Bianca!” on the screen. To accomplish this goal, we have to override the java.applet.Applet class’s paint( ) function. This built-in paint( ) function of a class is called method in OOP term. A method is simply a function that is a member of the class.

Take note that we indicate that the paint( ) method is automatically passed one parameter - an object of the Graphics class called g. This Graphics object represents the physical display of the applet.

This further means that we can use the built-in methods of this object such as drawLine( ), drawImage( ), drawOval( ), drawString( ) and others, to draw on the screen. In our case, we want to place the string “Hello Bianca!” on the screen. To accomplish this task, we apply the drawString( ) method. The question is “How can we reach the methods of an object like the Graphics object named g? We can accomplish that with a dot operator (.) like this syntax:

g.drawString( )

where here we are invoking g’s drawString( ) method to draw a string of text on the screen. We supply the three parameters to the drawString ( ) method - first is the string of text we want to display, then the (x,y) location of that string in our applet which is measured in pixel.

The coordinate system in Java program is set up with the origin (0,0) at the upper-left, with x increasing horizontally to the right, and y increasing vertically downwards. The Java programming convention is like in C and C++ where a semicolon ( ; ) indicates the end of a program statement.

At last, we have completed the code necessary for this applet, which in turn generated the new class - hello1. When the Java compiler creates hello1.class, the entire specification of the new class will be in that file. This is the actual binary file that we upload to the Internet Service Provider (ISP) so that it will be included in our Web page. A Java-enabled Web browser such as Internet Explorer (IE) or Netscape Navigator takes this Java class specification and creates an object of that class and then gives it control to display itself, and in some cases, handles user’s input and output tasks.

Example 2:
Design and develop a Java program that will display the message, “You are welcome Bianca!” at the text field on the applet. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.*;
public class text1 extends java.applet.Applet
{
TextField txtText1;
public void init()
{
txtText1 = new TextField(20);
txtText1.setText("You are welcome Bianca!");
}
}

2. Then save the Java program with the filename: text1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>text1</title>
<body>
<hr>
<applet
code=text1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: text1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac text1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\text1.htm

Explanation:
Here, we import all the classes in the Java AWT (Abstract Windowing Toolkit) package by using the import java.awt.* statement declaration at the top of our program.
Code:
import java.awt.*;
public class text1 extends java.applet.Applet
{
TextField txtText1;

These are the classes that will let us add command buttons and text fields to our applet. For example, two of the classes in this package that we will use are Button and TextField.
You will notice that we declared a new public class named text1 as our applet’s main class, which is derived from the java.applet.Applet base class.

Next, we created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement.
Code:
    public void init( )
{
txtText1 = new TextField(20);
txtText1.setText("You are welcome Bianca!");
}

When we want to initialize an applet, for example, adding a text field to it, we can accomplish that in the init( ) method. All kinds of object initialization take place at this method. This method runs automatically when the applet starts. This is the reason why we have to place all the code we want to run first in the init( ) method.

To develop this method, we simply construct it using the public void init ( ) statement such as the following:
Code:
public void init( )
{
// Place all your initialization code here:
}

Remember that a method is simply a function that is a member of the class. The init( ) function (method) is like any other function or method, except that it doesn’t return a value. This is the reason why we have the return type void in our program statement.

Our task in the init( ) function is to create the new text field and design it in our applet. Creating controls or objects in Java programming is a two-step process. The first one is to declare the new object and secondly, create the new object in the init( ) method using the Java programming language’s new operator.

This operator is used to allocate memory for objects, variables, and arrays.

We make the text field a 20 characters wide, so we pass a value of 20 to the TextField class’s constructor. The purpose of the constructor is to initialize the object we want. A class constructor is called in our program when a new object is being created of that class, and we can set the object up as we like it.

Since a constructor is a method, we can pass data to a constructor. In this example, we pass a value of 20 to our new text field’s constructor.

We add the text field to the applet’s display by using the add( ) method, wherein we add the new control txtText1 to the applet’s default layout. Finally, we will add the text string to the text field object using the setText( ) method of the TextField class.

Note:
This // (symbol) is a program comment. Meaning, it is ignored by the Java compiler during program compilation. Comments or comment will make our program clearer to understand and easier to debug and maintain once we go back over it for some months that passed.

Example 3:
Design and develop a Java program that when the user clicks the Click Me button, the message “Click Me button clicked!” will be displayed at the text field. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button1 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1;
public void init() {
txtText1 = new TextField(20);
cmdButton1= new Button("Click Me!");
}
public void actionPerformed(ActionEvent objEvent) {
String strMessage = new String ("Click Me button clicked!");
if (objEvent.getSource() == cmdButton1) {
txtText1.setText(strMessage);
}
}
}

2. Then save the Java program with the filename: button1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>button1</title>
<body>
<hr>
<applet
code=button1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: button1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac button1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\button1.htm
Explanation:
We import here the java.awt.event.* packaged that contains the ActionListener interface, as you can see its declaration below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

We created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement and the command button object cmdButton1 of the Button class by using the declaration Button cmdButton1 statement.

Events such as click event of the command button are passed from source controls to listener objects. This is the reason why we connect a listener to the command button - cmdButton1.

We can find out what events occur such as button clicks or mouse movements, using the ActionListener interface. To use the delegation-based event model technique, we specify that the applet will implement the ActionListener interface in our program as what you can observe in our code.

When an event occurs, the listener object can hear it, so we will make the listener object the applet object itself by connecting the applet to cmdButton1 as a listener.

We can accomplish that with the command button’s addActionListener( ) method, as what you can see in our syntax implementation below:
Code:
 import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button1 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1;
public void init( ) {
txtText1 = new TextField(20);
cmdButton1= new Button("Click Me!");
}

Now to indicate that we want the applet itself to be the button’s listener, we need to be able to pass the applet itself as an argument to the addActionListener( ) method. To accomplish that, we pass the this parameter into it.

When there are command button events (such as a click from the mouse or a key-press from the keyboard) that will happen, they will be sent to the applet, because we have already set up the ActionListener( ) method in our program.

To make use of the command button events, we will catch the events sent to us by overriding the ActionListener interface’s actionPerformed( ) method and adding our own version, such as the following:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button1 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1;
public void init() {
txtText1 = new TextField(20);
cmdButton1= new Button("Click Me!");
}
public void actionPerformed(ActionEvent objEvent) {

The actionPerformed( ) method is the method that will be called when the user clicks the command button in the applet. The ActionEvent object that is passed as a parameter here holds information about the event that occurred. This is important if we have two or more command buttons in our applet. How can we make sure that command button 1 or command button 2 was clicked?

We can check which command button was clicked with the getSource( ) method of the ActionEvent class. The getSource( ) method returns the control that caused the event.

We can check to see if that control is a command button1 with the help of if conditional statement, like this:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button1 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1;
public void init() {
txtText1 = new TextField(20);
cmdButton1= new Button("Click Me!");
}
public void actionPerformed(ActionEvent objEvent) {
String strMessage = new String ("Click Me button clicked!");
if (objEvent.getSource( ) == cmdButton1) {
txtText1.setText(strMessage);
}
}
}

If the control that created the Java event is button1, then the code that follows the if conditional statement will be executed when the conditional expression is evaluated to true. In this case, the message “Click Me button clicked!” will be displayed at the text field.

You will notice here that we set up the Java string object named strMessage as a local variable under the actionPerformed method and will only be available within this method (function). This is a good programming practice, since the locally declared strMessage variable will be in no way can interfere or in conflict with other variable in other methods or functions. Finally, the text string value that the strMessage variable holds will be displayed in the text field using the setText( ) method of the TextField class.

Example 4:
Design and develop a Java program that if the user clicks the command button Button1, the message “Button1 clicked!” will be displayed at the text field or if the user clicks the command button Button2, the message “Button2 clicked!” will be displayed instead. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button2 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1, cmdButton2;
public void init() {
txtText1 = new TextField(20);
cmdButton1= new Button("Button1");
cmdButton2 = new Button("Button2");
}
public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource() == cmdButton1) {
txtText1.setText("Button1 clicked!");
}
if (objEvent.getSource() ==cmdButton2) {
txtText1.setText("Button2 clicked!");
}
}
}

2. Then save the Java program with the filename: button2.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>button2</title>
<body>
<hr>
<applet
code=button2.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: button2.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac button2.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\button2.htm

Explanation:
Again, we import here the java.awt.event.* package that contains the ActionListener interface, as you can see its declaration below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

We created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement and the command button objects cmdButton1 and cmdButton2 of the Button class by using the declaration Button cmdButton1, cmdButton2 statement.

We can find out what events occur such as button clicks or mouse movements, using the ActionListener interface. To use the delegation-based event model technique, we specify that the applet will implement the ActionListener interface in our program as what you can observe in our code.

When an event occurs, the listener object can hear it, so we will make the listener object the applet object itself by connecting the applet to cmdButton1 and cmdButton2 as the listeners. We can accomplish that with the command button’s addActionListener( ) method, as what you can see in our syntax implementation below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button2 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1, cmdButton2;
public void init( ) {
txtText1 = new TextField(20);
cmdButton1= new Button("Button1");
cmdButton2 = new Button("Button2");
}

The actionPerformed( ) method is the method that will be called when the user clicks the command button in the applet. The ActionEvent object that is passed as a parameter here holds information about the event that occurred. This is important if we have two or more command buttons in our applet. How can we make sure that command button 1 or command button 2 was clicked?

We can check which command button was clicked with the getSource( ) method of the ActionEvent class. The getSource( ) method returns the control that caused the event. We can check to see if that control is a command button 1 (cmdButton1) or command button 2 (cmdButton2) with the help of if conditional statement, like this:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class button2 extends Applet implements ActionListener {
TextField txtText1;
Button cmdButton1, cmdButton2;
public void init() {
txtText1 = new TextField(20);
cmdButton1= new Button("Button1");
cmdButton2 = new Button("Button2");
}
public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource( ) == cmdButton1) {
txtText1.setText("Button1 clicked!");
}
if (objEvent.getSource( ) ==cmdButton2) {
txtText1.setText("Button2 clicked!");
}
}
}

If the control that created the Java event is command button 1 (cmdButton1), then the code that follows its corresponding if conditional statement will be executed when the conditional expression is evaluated to true. In this case, the message “Button1 clicked!” will be displayed at the text field. Now if the Java event is command button 2 (cmdButton2), then the code that follows its corresponding if conditional statement will be executed when the conditional expression is evaluated to true. So the message “Button2 clicked!” will be displayed instead.

The message can be displayed in the text field using the setText( ) method of the TextField class.
Example 5:
Design and develop a Java program that when the user clicks the Click Me button, the message “I love you very much, Bianca! Promise.” will be displayed at the text area. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class textarea1 extends Applet implements ActionListener {
TextArea txtTextArea1;
Button cmdButton1;
public void init() {
txtTextArea1 = new TextArea(" ",5,25);
cmdButton1= new Button("Click Me!");
}
public void actionPerformed(ActionEvent objEvent) {
String strMessage="I love you very much, Bianca! Promise.";
if (objEvent.getSource() == cmdButton1) {
txtTextArea1.insert(strMessage,0);
}
}
}

2. Then save the Java program with the filename: textarea1.java.

3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>textarea1</title>
<body>
<hr>
<applet
code=textarea1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: textarea1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac textarea1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\textarea1.htm

Explanation:
The text area control is preferably used when we have multiple lines of text to display such as a group of sentences or one paragraph of information. The text area object can be set up using the TextArea class of the Java programming language as you will observe later in our discussion.

Again, we import here the java.awt.event.* package that contains the ActionListener interface, as you can see its declaration below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

We created the text area object txtTextArea1 of the TextArea class by using the declaration TextArea txtTextArea1 statement and the command button object cmdButton1 of the Button class by using the declaration Button cmdButton1 statement.

Events such as click event of the command button are passed from source controls to listener objects. This is the reason why we connect a listener to the command button - cmdButton1.

We can find out what events occur such as button clicks or mouse movements, using the ActionListener interface. To use the delegation-based event model technique, we specify that the applet will implement the ActionListener interface in our program as what you can observe in our code.

When an event occurs, the listener object can hear it, so we will make the listener object the applet object itself by connecting the applet to cmdButton1 as a listener.

We can accomplish that with the command button’s addActionListener( ) method, as what you can see in our syntax implementation below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class textarea1 extends Applet implements ActionListener {
TextArea txtTextArea1;
Button cmdButton1;
public void init( ) {
txtTextArea1 = new TextArea(" ",5,25);
cmdButton1= new Button("Click Me!");
}

We handle text area the same way we handle text field. However, we specify three parameters to the TextArea constructor.

The first parameter we specify is an empty string, so that initially the text area will appear blank, then the last two numeric parameters specified the 5 rows and 25 columns text area’s height and width in the applet portion.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class textarea1 extends Applet implements ActionListener {
TextArea txtTextArea1;
Button cmdButton1;
public void init( ) {
txtTextArea1 = new TextArea(" ",5,25);
cmdButton1= new Button("Click Me!");
}
public void actionPerformed(ActionEvent objEvent) {
String strMessage="I love you very much, Bianca! Promise.";
if (objEvent.getSource() == cmdButton1) {
txtTextArea1.insert(strMessage,0);
}
}
}

The actionPerformed( ) is the method that will be called when the user clicks the command button in the applet. The ActionEvent object that is passed as a parameter here holds information about the event that occurred.
This is important if we have two or more command buttons in our applet. How can we make sure that command button 1 or command button 2 was clicked?

We can check which command button was clicked with the getSource( ) method of the ActionEvent class. The getSource( ) method returns the control that caused the event.

We can check to see if that control is a command button1 with the help of if conditional statement.

If the control that created the Java event is button1, then the code that follows the if conditional statement will be executed when the conditional expression is evaluated to true. In this case, the message “I love you very much, Bianca! Promise.” will be displayed at the text area.

You will notice here that we set up the Java string object named strMessage as a local variable under the actionPerformed method and will only be available within this method (function). This is a good programming practice, since the locally declared strMessage variable will be in no way can interfere or in conflict with other variable in other methods or functions. Instead of using setText( ) to set the text string of our text area as we did for text field, we will use insert( ) method of the TextArea class.

In this way, we can insert the text string at a specified location in the text area. Here in our example, we place the “I love you very much, Bianca!.

Promise.” message at the beginning of the text area since we pass a location of 0 parameter value as you will notice at the code above.

Example 6:
Design and develop a Java program that allows you to both enter text string into a text box and display the text string on the applet. Follow the design specification below:

Figure 1.6 An applet with a text string entered and displayed both at the text box and on applet.

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string3 extends Applet implements TextListener {
Label lblMessage;
TextField txtText1;
String strString1 = "";

public void init() {
lblMessage = new Label("Enter some words:  ");
txtText1 = new TextField(20);
}
public void paint(Graphics objG) {
objG.drawString(strString1,202,42);
}
public void textValueChanged(TextEvent objE) {
strString1 = txtText1.getText();
repaint();
}
}

2. Then save the Java program with the filename: string3.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>string3</title>
<body>
<hr>
<applet
code=string3.class
width=600
height=100>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: string3.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac string3.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\string3.htm

Explanation:
Again, we import here the java.awt.event.* package that contains the TextListener interface, as you can see its declaration below:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string3 extends Applet implements TextListener {
Label lblMessage;
TextField txtText1;
String strString1 = "";

We created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement and label object lblMessage of the Label class by using the declaration Label lblMessage1 statement. We also add here our declaration of the variable string strString1 to handle the entered text string as our data to be displayed to the applet. We initially declared it as an empty string.
Code:
public void init( ) {
lblMessage = new Label("Enter some words:  ");
txtText1 = new TextField(20);
}

The lines above simply add the label “Enter some words:” to the applet, as well as a new text-entry field of 20 characters. We use the TextListener interface so that whatever text string is entered by the user at the text field, it will be displayed right away at the applet (as you will observe while running the applet).
Code:
public void paint(Graphics objG) {
objG.drawString(strString1,202,42);
}
public void textValueChanged(TextEvent objE) {
strString1 = txtText1.getText( );
repaint( );
}
}
You will notice in our code above that in the event there is a change in value at the text field , the value (text string) entered will be stored right at the strString1 variable which in turn is displayed at the applet using the drawString( ) method.

Example 7:
Design and develop a Java program that allows you to both enter text string into a text box and display the text string on the applet. This time, the font size of the displayed text string on the applet is changed to 20 and its font is Georgia. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string4 extends Applet implements TextListener {
Label lblMessage;
TextField txtText1;
String strString1 = "";
Font fntFont = new Font("Georgia",Font.BOLD,20);

public void init() {
lblMessage = new Label("Enter some words:  ");
txtText1 = new TextField(20);
}

public void paint(Graphics objG) {
objG.setFont(fntFont);
objG.drawString(strString1,202,62);
}

public void textValueChanged(TextEvent objE) {
strString1 = txtText1.getText();
repaint();
}
}

2. Then save the Java program with the filename: string4.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>string4</title>
<body>
<hr>
<applet
code=string4.class
width=600
height=100>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: string4.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac string4.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\string4.htm

Explanation:
As usual, we import here the java.awt.event.* packaged that contains the TextListener interface, as you can see its declaration below:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string4 extends Applet implements TextListener {
Label lblMessage;
TextField txtText1;
String strString1 = "";
Font fntFont = new Font("Georgia",Font.BOLD,20);

We created the text box object txtText1 of the TextField class by using the declaration TextField txtText1 statement and label object lblMessage of the Label class by using the declaration Label lblMessage1 statement. We also add here our declaration of the variable string strString1 to handle the entered text string as our data to be displayed to the applet.

We initially declared it as an empty string. What is noticeable here in this example is our declaration of the font object using the Font class, wherein the parameters we specify are the new font’s font style (Georgia), its font size (20), and the font must be written in bold (BOLD).
Code:
public void init( ) {
lblMessage = new Label("Enter some words:  ");
txtText1 = new TextField(20);
}

The lines above simply add the label “Enter some words:” to the applet, as well as a new text-entry field of 20 characters. We use the TextListener interface so that whatever text string is entered by the user at the text field, it will be displayed right away at the applet (as you will observe while running the applet).

Code:
public void paint(Graphics objG) {
objG.setFont(fntFont);
objG.drawString(strString1,202,62);
}

public void textValueChanged(TextEvent objE) {
strString1 = txtText1.getText( );
repaint( );
}
}

You will notice in our code above that in the event there is a change in value at the text field , the value (text string) entered will be stored right at the strString1 variable which in turn is displayed at the applet using the drawString( ) method. Setting the font style to format the displayed text string is so easy by using the setFont( ) method. We simply pass the object name to that method.

Example 8:
Design and develop a Java program that allows you to both enter text string into a text box and display the text string on the applet. The text string will be displayed only after the Enter key is pressed. Follow the design specification below:

Figure 1.8 An applet with a text string entered and displayed after pressing the Enter key.

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string1 extends Applet implements ActionListener {
Label lblMessage;
TextField txtText1;
String  strString1 = "";

public void init() {
lblMessage = new Label("Input some words, then press the Enter key:");
txtText1 = new TextField(15);
}
public void paint(Graphics objG) {
if (strString1 !="") {
objG.drawString("You entered:  "+strString1,21,71);
}
}
public void actionPerformed(ActionEvent objE) {
strString1 = txtText1.getText();
repaint();
}
}

2. Then save the Java program with the filename: string1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>string1</title>
<body>
<hr>
<applet
code=string1.class
width=300
height=100>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: string1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac string1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\string1.htm

Explanation:
We created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement and label object lblMessage of the Label class by using the declaration Label lblMessage1 statement. We also add here our declaration of the variable string strString1 to handle the entered text string as our data to be displayed to the applet.

We initially declared it as an empty string, as you can see in our code below:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string1 extends Applet implements ActionListener {
Label lblMessage;
TextField txtText1;
String  strString1 = " ";

We can find out what events occur such as key-presses, button-clicks or mouse-movements, using the ActionListener interface. To use the delegation-based event model technique, we specify that the applet will implement the ActionListener interface in our program.

When an event occurs, the listener object can hear it, so we will make the listener object the applet object itself by connecting the applet to txtText1 as a listener. We can accomplish that with the text field’s addActionListener( ) method, as what you can see in our syntax implementation below:
Code:
public void init( ) {
lblMessage = new Label("Input some words, then
press the Enter key:");
txtText1 = new TextField(15);
}
You will notice in our code below that in the event there is a change in value at the text field , the value (text string) entered will be stored right at the strString1 variable which in turn is displayed at the applet using the drawString( ) method:
Code:
public void paint(Graphics objG) {
if (strString1 !=" ")  {
objG.drawString("You entered:  "+strString1,21,71);
}
}
public void actionPerformed(ActionEvent objE) {
strString1 = txtText1.getText( );
repaint( );
}
}
What is noticeable here in our code is that the text string will be displayed only if strString1 variable is not equal to an empty string. The plus (+) sign in our drawString( ) method means to cancatenate the string “You entered:” to whatever string value the strString1 variable is containing.

Example 9:
Design and develop a Java program that allows you to both enter text string into a text box and display the text string on the applet. The text string will be displayed only after the Enter key is pressed. This time, the font size of the displayed text string on the applet is changed to 20 and its font is Georgia. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string2 extends Applet implements ActionListener {
Label lblMessage;
TextField txtText1;
String  strString1 = "";
Font fntFont= new Font("Georgia",Font.BOLD,20);

public void init() {
lblMessage = new Label("Input some words, then
press the Enter key:");
txtText1 = new TextField(20);
}

public void paint(Graphics objG) {
if (strString1 !="") {
objG.setFont(fntFont);
objG.drawString("You entered:  "+strString1,21,61);
}
}
public void actionPerformed(ActionEvent objE) {
strString1 = txtText1.getText();
repaint();
}
}

2. Then save the Java program with the filename: string2.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
Code:
<html>
<!- Web page written with Java Applet>
<title>string2</title>
<body>
<hr>
<applet
code=string2.class
width=600
height=100>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: string2.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac string2.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following at your Web browser:
C:\javaprog\string2.htm
Explanation:
We created the text field object txtText1 of the TextField class by using the declaration TextField txtText1 statement and label object lblMessage of the Label class by using the declaration Label lblMessage1 statement. We also add here our declaration of the variable string strString1 to handle the entered text string as our data to be displayed to the applet. We initially declared it as an empty string. What is noticeable here in this example is our declaration of the font object using the Font class, wherein the parameters we specify are the new font’s font style (Georgia), its font size (20), and the font must be written in bold (BOLD) as you can see in our code below:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class string2 extends Applet implements ActionListener {
Label lblMessage;
TextField txtText1;
String  strString1 = " ";
Font fntFont= new Font("Georgia",Font.BOLD,20);
We can find out what events occur such as key-presses, button-clicks or mouse-movements, using the ActionListener interface. To use the delegation-based event model technique, we specify that the applet will implement the ActionListener interface in our program. When an event occurs, the listener object can hear it, so we will make the listener object the applet object itself by connecting the applet to txtText1 as a listener. We can accomplish that with the text field’s addActionListener( ) method, as what you can see in our syntax implementation below:
Code:
public void init( ) {
lblMessage = new Label("Input some words, then
press the Enter key:");
txtText1 = new TextField(20);
}

You will notice in our code below that in the event there is a change in value at the text field , the value (text string) entered will be stored right at the strString1 variable which in turn is displayed at the applet using the drawString( ) method:
Code:
public void paint(Graphics objG) {
if (strString1 !=" ") {
objG.setFont(fntFont);
objG.drawString("You entered:  "+strString1,21,61);
}
}
public void actionPerformed(ActionEvent objE) {
strString1 = txtText1.getText();
repaint();
}
}

Setting the font style to format the displayed text string is so easy by using the setFont( ) method. We simply pass the object name to that method.

Java Lecture Pages