Java Lecture Chapter 2

Marvic

Member
Using Check Boxes and Option Buttons
Check boxes are valid as single controls, however they are not mutually exclusive. Meaning, the users can check as many check boxes as they want, unlike in Option buttons, the user can only select one option at a time. Check boxes are applicable to the situation where you can select a lot of items because you want them all, such as ordering a pizza and choose all the ingredients you want to put on it as long as you can afford to pay. While an option buttons, you can only choose one and only one.

The option buttons are applicable to this situation - marrying a girl. In the Christian world, no matter how many girlfriends you have at the same time and even in the same place, you can choose only one among them to marry.
Let us now have an example that deals with these controls to learn them in action.

Example 1:
Design and develop a Java program that when the user clicks one of the three check boxes, it will indicate in the text field on which check box the user had clicked. For example, if Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field. It will do the same with Check box 1 and Check box 3. 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 checkbox1 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1;
  public void init() {
    chkBox1 = new Checkbox("Check 1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Check 2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Check 3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
   }
   public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText1.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText1.setText("Check box 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: checkbox1.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>
<head>
<title>checkbox1</title>
</head>
<body>
<hr>
<applet
code=checkbox1.class
width=300
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: checkbox1.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 checkbox1.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\checkbox1.htm

Explanation:
We created a new class named checkbox1 as shown below:

Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox1 extends Applet implements ItemListener {

Now we can declare the three check boxes and named them, chkBox 1, chkBox 2, and chkBox 3 and text field called txtText1. The txtText1 object is used to display which check box is being currently clicked by the user. The check boxes are created with the Java Checkbox class, while the text field is created with the Java TextField class as follows:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox1 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1;

You will notice that we actually created and add the new check boxes to the applet in the init( ) method as you can see below:
Code:
public class checkbox1 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1;
  public void init( ) {
    chkBox1 = new Checkbox("Check 1");
    add(chkBox1);                                 label                                 
    chkBox1.addItemListener(this);     
    chkBox2 = new Checkbox("Check 2");     
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Check 3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
   }

Take note that we gave a label to each and every check box by passing a string to the constructor of the Checkbox class, as we did above. To connect the code of the check boxes controls, we use the ItemListener instead of the ActionListener which we used previously with the command button control. The check box need the ItemListener since it can be either checked or unchecked.

We can make our applet into the listener for the check boxes with the check box addItemListener( ) method as what we can see at the above code. The addItemListener( ) method adds the given item listener to receive item events from the check box.

The method to override when using the ItemListener interface is the itemStateChange( ) method. This method is called when the user clicks one of our check boxes and we are passing an object of the class ItemEvent in that method. We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class as what you can see in our code below:
Code:
public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox2) {
       txtText1.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox3) {
       txtText1.setText("Check box 3 selected!");
      }
  }
}

If chkBox1 is clicked, we can place a message in the text field that says: “ Check box 1 selected!” using the setText( ) method of the TextField class. We do the same thing with other check boxes too.
You will notice that we increase the width of our applet here in this example (300 wide and 200 high in pixel) to simply accommodate the three check boxes, as you can see in our HTML script below:
Code:
<applet
code=checkbox1.class
width=300
height=200>
</applet>

By doing so, we can properly display the check boxes and text field in our applet.

Example 2:
Design and develop a Java program that when the user clicks one of the three check boxes, it will indicate in the text field on which check box the user had clicked. For example, if Check box 1 was clicked by the user, it will display “Check box 1 selected!” at the text field 1. If Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field 2, and if Check box 3 was clicked by the user, it will display “Check box 3 selected!” at the text field 3. 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 checkbox2 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;
    public void init() {
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    txtText2 = new TextField(19);
    add(txtText2);
    txtText3 = new TextField(19);
    add(txtText3);
    }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: checkbox2.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>
<head>
<title>checkbox2</title>
</head>
<body>
<hr>
<applet
code=checkbox2.class
width=200
height=200>
</applet>
<hr>
</body>
</html>
/[CODE]

4. Now save the HTML script with the filename: checkbox2.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 checkbox2.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\checkbox2.htm

Explanation:
Here in our example, we created a new class named checkbox2 and declared three check boxes and named them, chkBox1, chkBox2, and chkBox3. Then we declared three text fields called txtText1, txtText2, and txtText3. The txtText1, txtText2, and txtText3 objects are used to report which of the check boxes is currently clicked by the user. The check boxes are created with the Java Checkbox class, while the text fields are created with the Java TextField class as you can observe in our code below:
[CODE]
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox2 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;

You will notice that we actually created and add the new check boxes and text fields to the applet in the init( ) method as you can see below:
Code:
public class checkbox2 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;   label
    public void init( ) {                             
    chkBox1 = new Checkbox("Chk1");     
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    txtText2 = new TextField(19);
    add(txtText2);
    txtText3 = new TextField(19);
    add(txtText3);
    }

Take note that we gave a label to each and every check box by passing a string to the constructor of the Checkbox class, as we did above. To connect the code of the check boxes controls, we use the ItemListener instead of the ActionListener which we used previously with the command button control. The check box need the ItemListener since it can be either checked or unchecked.

We can make our applet into the listener for the check boxes with the check box addItemListener( ) method as what we can see at the above code.

The addItemListener( ) method adds the given item listener to receive item events from the check box.
The method to override when using the ItemListener interface is the itemStateChange( ) method. This method is called when the user clicks one of our check boxes and we are passed an object of the class ItemEvent in that method. We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class as what you can see in our code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
 }
}
If chkBox1 is clicked, we can place a message in the text field that says: “ Check box 1 selected!” using the setText( ) method of the TextField class. We do the same thing with other check boxes too.


Example 3:
Design and develop a Java program that when the user clicks one of the three check boxes, it will indicate in the text field on which check box the user had clicked. For example, if Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field. It will do the same with Check box 1 and Check box 3. This example is similar to Example 1, however, the text field is now position at the top of the three check boxes. 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 checkbox3 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1;
  public void init() {
    txtText1 = new TextField(19);
    add(txtText1);
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
   }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText1.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText1.setText("Check box 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: checkbox3.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>
<head>
<title>checkbox3</title>
</head>
<body>
<hr>
<applet
code=checkbox3.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: checkbox3.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 checkbox3.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\checkbox3.htm

Explanation:
Here in our example, we created a new class named checkbox3 and declared three check boxes and named them, chkBox1, chkBox2, and chkBox3. Then we declared one text field called txtText1. The txtText1 object is used to report which of the check boxes is currently clicked by the user. The check boxes are created with the Java Checkbox class, while the text field is created with the Java TextField class as you can observe in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox3 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1;

You will notice that we actually created and add the new check boxes and text field to the applet in the init( ) method as you can see below:
Code:
  public void init( ) {
    txtText1 = new TextField(19);      label
    add(txtText1);                               
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
   }

What is noticeable here in this example is that we first declare the text field object before declaring the three check boxes. This is the reason why the text field is displayed at the top location in our applet, while the three check boxes are displayed below it.

Take note that we gave a label to each and every check box by passing a string to the constructor of the Checkbox class, as we did above. To connect the code of the check boxes controls, we use the ItemListener instead of the ActionListener which we used previously with the command button control. The check box need the ItemListener since it can be either checked or unchecked.

We can make our applet into the listener for the check boxes with the check box addItemListener( ) method as what we can see at the above code. The addItemListener( ) method adds the given item listener to receive item events from the check box.

The method to override when using the ItemListener interface is the itemStateChange( ) method. This method is called when the user clicks one of our check boxes and we are passed an object of the class ItemEvent in that method. We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class as what you can see in our code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText1.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText1.setText("Check box 3 selected!");
      }
  }
}
If chkBox1 is clicked, we can place a message in the text field that says: “ Check box 1 selected!” using the setText( ) method of the TextField class.

We do the same thing with other check boxes too.

Example 4:
Design and develop a Java program that when the user clicks one of the three check boxes, it will indicate in the text field on which check box the user had clicked. For example, if Check box 1 was clicked by the user, it will display “Check box 1 selected!” at the text field 1. I Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field 2, and if Check box 3 was clicked by the user, it will display “Check box 3 selected!” at the text field 3. This is the same with Example 2, however the check boxes and text fields here are positioned at the applet in an alternate position. 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 checkbox4 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;
  public void init() {
    chkBox1 = new Checkbox("Check 1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    chkBox2 = new Checkbox("Check 2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    txtText2 = new TextField(19);
    add(txtText2);
    chkBox3 = new Checkbox("Check 3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText3 = new TextField(19);
    add(txtText3);
    }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: checkbox4.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>
<head>
<title>checkbox4</title>
</head>
<body>
<hr>
<applet
code=checkbox4.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: checkbox4.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 checkbox4.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\checkbox4.htm
Explanation:

Here in our example, we created a new class named checkbox4 and declared three check boxes and named them, chkBox1, chkBox2, and chkBox3. Then we declared three text fields called txtText1, txtText2, and txtText3. The txtText1, txtText2, and txtText3 objects are used to report which of the check boxes is currently clicked by the user. The check boxes are created with the Java Checkbox class, while the text fields are created with the Java TextField class as you can observe in our code below:

Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox4 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;

You will notice that we actually created and add the new check boxes and text fields to the applet in the init( ) method as you can see below:
Code:
  public void init( ) {
    chkBox1 = new Checkbox("Check 1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    chkBox2 = new Checkbox("Check 2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    txtText2 = new TextField(19);       label
    add(txtText2);                                 
    chkBox3 = new Checkbox("Check 3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText3 = new TextField(19);
    add(txtText3);
    }

What is noticeable here in this example is that we declare the check box object and text field alternately. This is the reason why the check box and text field are displayed at the applet alternately.

Take note that we gave a label to each and every check box by passing a string to the constructor of the Checkbox class, as we did above. To connect the code of the check boxes controls, we use the ItemListener instead of the ActionListener which we used previously with the command button control. The check box need the ItemListener since it can be either checked or unchecked.

We can make our applet into the listener for the check boxes with the check box addItemListener( ) method as what we can see at the above code. The addItemListener( ) method adds the given item listener to receive item events from the check box.

The method to override when using the ItemListener interface is the itemStateChange( ) method. This method is called when the user clicks one of our check boxes and we are passed an object of the class ItemEvent in that method. We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class as what you can see in our code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
  }
}
f chkBox1 is clicked, we can place a message in the text field that says: “ Check box 1 selected!” using the setText( ) method of the TextField class. We do the same thing with other check boxes too.

Example 5:
Design and develop a Java program that when the user clicks one of the three check boxes, it will indicate in the text field on which check box the user had clicked. For example, if Check box 1 was clicked by the user, it will display “Check box 1 selected!” at the text field 1. If Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field 2, and if Check box 3 was clicked by the user, it will display “Check box 3 selected!” at the text field 3.

This is the same with Example 4, however the check boxes and text fields here are positioned in the same line with each other. 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 checkbox5 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;
  public void init() {
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    txtText2 = new TextField(19);
    add(txtText2);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText3 = new TextField(19);
    add(txtText3);
    }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: checkbox5.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>
<head>
<title>checkbox5</title>
</head>
<body>
<hr>
<applet
code=checkbox5.class
width=250
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: checkbox5.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 checkbox5.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\checkbox5.htm

Explanation:
Here in our example, we created a new class named checkbox5 and declared three check boxes and named them, chkBox1, chkBox2, and chkBox3. Then we declared three text fields called txtText1, txtText2, and txtText3. The txtText1, txtText2, and txtText3 objects are used to report which of the check boxes is currently clicked by the user. The check boxes are created with the Java Checkbox class, while the text fields are created with the Java TextField class as you can observe in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class checkbox5 extends Applet implements ItemListener {
  Checkbox  chkBox1, chkBox2, chkBox3;
  TextField txtText1, txtText2, txtText3;

You will notice that we actually created and add the new check boxes and text fields to the applet in the init( ) method as you can see below:
Code:
  public void init( ) {
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    txtText1 = new TextField(19);
    add(txtText1);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    txtText2 = new TextField(19);
    add(txtText2) ;                               label
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText3 = new TextField(19);
    add(txtText3);
    }

What is noticeable here in this example is that we declare the check box object and text field alternately. This is the reason why the check box and text field are displayed at the applet alternately. Now to make the check box and text field to be displayed in the same line in our applet, we have to adjust the width of the applet into 250 wide and 200 high in pixel as you will notice here in our HTML script below:
Code:
<applet
code=checkbox5.class
width=250
height=200>
</applet>

Take note that we gave a label to each and every check box by passing a string to the constructor of the Checkbox class, as we did above. To connect the code of the check boxes controls, we use the ItemListener instead of the ActionListener which we used previously with the command button control. The check box need the ItemListener since it can be either checked or unchecked.

We can make our applet into the listener for the check boxes with the check box addItemListener( ) method as what we can see at the above code. The addItemListener( ) method adds the given item listener to receive item events from the check box.

The method to override when using the ItemListener interface is the itemStateChange( ) method. This method is called when the user clicks one of our check boxes and we are passed an object of the class ItemEvent in that method. We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class as what you can see in our code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==chkBox1) {
       txtText1.setText("Check box 1 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable( )==chkBox3) {
       txtText3.setText("Check box 3 selected!");
      }
  }
}
If chkBox1 is clicked, we can place a message in the text field that says: “ Check box 1 selected!” using the setText( ) method of the TextField class. We do the same thing with other check boxes too.

Example 6:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. For example, if Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field. It will do the same with Option button 1 and Option button 3. 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 optionbutton1 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1;
    public void init() {
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Option 1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Option 2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Option 3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
   }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
      }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText1.setText("Option button 2 selected!");
      }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText1.setText("Option button 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: optionbutton1.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>
<head>
<title>optionbutton1</title>
</head>
<body>
<hr>
<applet
code=optionbutton1.class
width=300
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optionbutton1.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 optionbutton1.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\optionbutton1.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optionbutton1 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1;
When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
    public void init( ) {
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Option 1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Option 2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Option 3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
   }
This attaches the new option button to the check box group. The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group.

This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button was currently clicked. Here is its code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==optButton1) {
       txtText1.setText("Option button 1 selected!");
      }
      if (objEvent.getItemSelectable( )==optButton2) {
       txtText1.setText("Option button 2 selected!");
      }
      if (objEvent.getItemSelectable( )==optButton3) {
       txtText1.setText("Option button 3 selected!");
      }
  }
}
We can examine which check box was clicked by the user through the getItemSelectable( ) method of the ItemEvent class, as what you can see in our code above.

If optButton1 is clicked, we can place a message in the text field that says: “ Option button 1 selected!” using the setText( ) method of the TextField class. We do the same thing with other check boxes too.
You will notice here that we increase the applet’s width to 300 (as you can see the HTML script below), because we want to accommodate the three option buttons at the top position. With this adjustment, the option buttons and the text field will be displayed in the applet properly.
Code:
<applet
code=optionbutton1.class
width=300
height=200>
</applet>
That’s the end of our discussion of this example about option buttons control.

Example 7:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. For example, if Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field. It will do the same with Option button 1 and Option button 3. This time, the position of the option buttons and text field are in reverse order. 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 optionbutton2 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1;
  public void init() {
    txtText1 = new TextField(20);
    add(txtText1);
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
   }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
      }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText1.setText("Option button 2 selected!");
      }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText1.setText("Option button 3 selected!");
      }
  }
}

2. Then save the Java program with the filename: optionbutton2.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>
<head>
<title>optionbutton2</title>
</head>
<body>
<hr>
<applet
code=optionbutton2.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optionbutton2.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 optionbutton2.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\optionbutton2.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optionbutton2 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1;

When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
  public void init( ) {
    txtText1 = new TextField(20);
    add(txtText1);
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
   }

This attaches the new option button to the check box group. The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group.

This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button was currently clicked. Here is its code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==optButton1) {
       txtText1.setText("Option button 1 selected!");
      }
      if (objEvent.getItemSelectable( )==optButton2) {
       txtText1.setText("Option button 2 selected!");
      }
      if (objEvent.getItemSelectable( )==optButton3) {
       txtText1.setText("Option button 3 selected!");
     }
  }
}
That’s the end of our discussion of this example about option buttons control.

Example 8:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. For example, if Option button 1 was clicked by the user, it will display “Option button 1 selected!” at the text field 1. If Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field 2. And if Option button 3 was clicked by the user, it will display “Option button 3 selected!” at the text field 3. 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 optionbutton3 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;
  public void init() {
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    txtText2 = new TextField(20);
    add(txtText2);
    txtText3 = new TextField(20);
    add(txtText3);     
 }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}

2. Then save the Java program with the filename: optionbutton3.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>
<head>
<title>optionbutton3</title>
</head>
<body>
<hr>
<applet
code=optionbutton3.class
idth=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optionbutton3.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 optionbutton3.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\optionbutton3.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optionbutton3 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;

When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
  public void init( ) {
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    txtText2 = new TextField(20);
    add(txtText2);
    txtText3 = new TextField(20);
    add(txtText3);     
 }

This attaches the new option button to the check box group.

You will notice here in the code above that we first initialize and add the three option buttons control then followed by the three text fields. In this way, the three option buttons are the first to be displayed in our applet, followed by the three text fields. Remember that these arrangement of control initialization in the init( ) method plays an important role in how the controls will be displayed at the applet. The first control to be initialized and added will be the first control also to be displayed at the applet. Meaning, the controls will be positioned at the top portion of the applet.

The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group. This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button was currently clicked. Here is its code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}

You will notice also in our code above that if the optButton1 is clicked by the user, the
if (objEvent.getItemSelectable( )==optButton1) {
conditional expression is evaluated to true, thus, executing its associated statement below:
Code:
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
The purpose why we set the txtText2 and txtText3 objects with an empty string is to display a blank to text field 2 and text field 3. We do the same thing with the other option buttons (the optButton2 and optButton3).

Example 9:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. For example, if Option button 1 was clicked by the user, it will display “Option button 1 selected!” at the text field 1. If Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field 2. And if Option button 3 was clicked by the user, it will display “Option button 3 selected!” at the text field 3. This time, option button and text field are in the same line with each other. 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 optionbutton4 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;
  public void init() {
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText3 = new TextField(20);
    add(txtText3);     
 }
 public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}

2. Then save the Java program with the filename: optionbutton4.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>
<head>
<title>optionbutton4</title>
</head>
<body>
<hr>
<applet
code=optionbutton4.class
width=250
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optionbutton4.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 optionbutton4.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\optionbutton4.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optionbutton4 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;

When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
  public void init( ) {
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText3 = new TextField(20);
    add(txtText3);     
}

This attaches the new option button to the check box group.
You will notice here in the code above that we initialize and add the Option button 1 control then followed by the Text field 1.The same goes with other option buttons and text fields. Remember that these arrangement of control initialization in the init( ) method plays an important role in how the controls will be displayed at the applet. The first control to be initialized and added will be the first control also to be displayed at the applet, followed by the other controls.

The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group. This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button was currently clicked. Here is its code below:
Code:
 public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}

You will notice also in our code above that if the optButton1 is clicked by the user, the
if (objEvent.getItemSelectable( )==optButton1) {
conditional expression is evaluated to true, thus, executing its associated statement below:
Code:
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");

The purpose why we set the txtText2 and txtText3 objects with an empty string is to display a blank to text field 2 and text field 3. We do the same thing with the other option buttons (the optButton2 and optButton3).
You will notice here that we increase the applet’s width to 250 (as you can see the HTML script below), because we want to accommodate the option button and the text field to be displayed in the applet within the same line. With this adjustment, the option buttons and the text fields will be displayed in the applet properly.
Code:
<applet
code=optionbutton4.class
width=250
height=200>
</applet>

That’s the end of our discussion of the fourth example of our option buttons control.

Example 10:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. For example, if Option button 1 was clicked by the user, it will display “Option button 1 selected!” at the text field 1. If Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field 2. And if Option button 3 was clicked by the user, it will display “Option button 3 selected!” at the text field 3. This time, option button and text field are in alternate order. 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 optionbutton5 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;
  public void init() {
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Option 1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    optButton2 = new Checkbox("Option 2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
    optButton3 = new Checkbox("Option 3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText3 = new TextField(20);
    add(txtText3);     
 }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}

2. Then save the Java program with the filename: optionbutton5.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>
<head>
<title>optionbutton5</title>
</head>
<body>
<hr>
<applet
code=optionbutton5.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optionbutton5.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 optionbutton5.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\optionbutton5.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optionbutton5 extends Applet implements ItemListener
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  TextField txtText1,txtText2,txtText3;

When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
  public void init( ) {
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Option 1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    optButton2 = new Checkbox("Option 2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
    optButton3 = new Checkbox("Option 3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
     txtText3 = new TextField(20);
    add(txtText3);     
}

This attaches the new option button to the check box group.

You will notice here in the code above that we initialize and add the Option button 1 control then followed by the Text field 1.The same goes with other option buttons and text fields. Remember that these arrangement of control initialization in the init( ) method plays an important role in how the controls will be displayed at the applet. The first control to be initialized and added will be the first control also to be displayed at the applet, followed by the other controls.

The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group. This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button was currently clicked. Here is its code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable( )==optButton1) {
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton2) {
       txtText2.setText("Option button 2 selected!");
       txtText1.setText(" ");
       txtText3.setText(" ");
      }
      if (objEvent.getItemSelectable( )==optButton3) {
       txtText3.setText("Option button 3 selected!");
       txtText1.setText(" ");
       txtText2.setText(" ");
      }
  }
}
You will notice also in our code above that if the optButton1 is clicked by the user, the
if (objEvent.getItemSelectable()==optButton1) {
conditional expression is evaluated to true, thus, executing its associated statement below:
Code:
       txtText1.setText("Option button 1 selected!");
       txtText2.setText(" ");
       txtText3.setText(" ");
The purpose why we set the txtText2 and txtText3 objects with an empty string is to display a blank to text field 2 and text field 3. We do the same thing with the other option buttons (the optButton2 and optButton3).

Example 11:
Design and develop a Java program that when the user clicks one of the three option buttons, it will indicate in the text field on which option button the user had clicked. The same thing also happened for the check boxes. For example, if Option button 1 was clicked by the user, it will display “Option button 1 selected!” at the text field 1. If Option button 2 was clicked by the user, it will display “Option button 2 selected!” at the text field 1. And if Option button 3 was clicked by the user, it will display “Option button 3 selected!” at the text field 1. In the case of the check boxes, if Check box 1 was clicked by the user, it will display “Check box 1 selected!” at the text field 2. If Check box 2 was clicked by the user, it will display “Check box 2 selected!” at the text field 2. And if Check box 3 was clicked by the user, it will display “Check box 3 selected!” at the text field 2. Follow the design specification below:

Figure 2.11 An applet with Option buttons, Check boxes and Text fields
Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optchk1 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  Checkbox chkBox1, chkBox2,chkBox3;
  TextField txtText1,txtText2;
  public void init() {
    optButtonGroup1 = new CheckboxGroup();
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    add(chkBox2);
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
 }
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
        }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText1.setText("Option button 2 selected!");
       }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText1.setText("Option button 3 selected!");
       }
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText2.setText("Check box 1 selected!");
        }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText2.setText("Check box 3 selected!");
      }
  }
 }

2. Then save the Java program with the filename: optchk1.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>
<head>
<title>optchk1</title>
</head>
<body>
<hr>
<applet
code=optchk1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: optchk1.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 optchk1.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\optchk1.htm

Explanation:
The option buttons act like the check boxes controls, however, unlike in check boxes where you can check them at the same time, in option buttons, you can only click one at a time. Using the CheckboxGroup object, we can associate option buttons with each other. Since option buttons are considered similar to check boxes in Java programming, therefore, the Checkbox class is used to declare the three option buttons we have created as you can see in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class optchk1 extends Applet implements ItemListener {
  CheckboxGroup  optButtonGroup1;
  Checkbox optButton1, optButton2, optButton3;
  Checkbox chkBox1, chkBox2,chkBox3;
  TextField txtText1,txtText2;

When we add the option buttons to a check box group, they change their appearance automatically to option buttons.

Now, we are ready to add the option buttons to the new check box group, CheckboxGroup and initialize them at the init( ) method. We can accomplish that when we create each option button, passing our CheckboxGroup object to the constructor of the Checkbox class as we can see here in our code below:
Code:
  public void init( ) {
    optButtonGroup1 = new CheckboxGroup( );
    optButton1 = new Checkbox("Opt1",false,optButtonGroup1);
    add(optButton1);
    optButton1.addItemListener(this);
    optButton2 = new Checkbox("Opt2",false,optButtonGroup1);
    add(optButton2);
    optButton2.addItemListener(this);
    optButton3 = new Checkbox("Opt3",false,optButtonGroup1);
    add(optButton3);
    optButton3.addItemListener(this);
    txtText1 = new TextField(20);
    add(txtText1);
    chkBox1 = new Checkbox("Chk1");
    add(chkBox1);
    chkBox1.addItemListener(this);
    chkBox2 = new Checkbox("Chk2");
    chkBox2.addItemListener(this);
    chkBox3 = new Checkbox("Chk3");
    add(chkBox3);
    chkBox3.addItemListener(this);
    txtText2 = new TextField(20);
    add(txtText2);
 }

This attaches the new option button to the check box group. Since we want to initialize and add three Check boxes in our applet, you will also notice that they appeared here in the init( ) method too.
As you will observe here in the code above that we initialize and add first the three Option buttons control then followed by the Text field 1, and we also do the same thing with the three Check boxes which we first initialize and add before the Text field 2. The arrangement of control initialization in the init( ) method plays an important role in how the controls will be displayed at the applet. The first control to be initialized and added will be the first control also to be displayed at the applet, followed by the other controls.
The addItemListener( ) method adds the given item listener to receive item events from the Checkbox group. This time we will add the itemStateChange( ) method to be used to report back to the user in our text field, indicating which option button or check box was currently clicked. Here is its code below:
Code:
  public void itemStateChanged(ItemEvent objEvent) {
      if (objEvent.getItemSelectable()==optButton1) {
       txtText1.setText("Option button 1 selected!");
        }
      if (objEvent.getItemSelectable()==optButton2) {
       txtText1.setText("Option button 2 selected!");
       }
      if (objEvent.getItemSelectable()==optButton3) {
       txtText1.setText("Option button 3 selected!");
       }
      if (objEvent.getItemSelectable()==chkBox1) {
       txtText2.setText("Check box 1 selected!");
        }
      if (objEvent.getItemSelectable()==chkBox2) {
       txtText2.setText("Check box 2 selected!");
      }
      if (objEvent.getItemSelectable()==chkBox3) {
       txtText2.setText("Check box 3 selected!");
      }
  }

This is the end of our discussion to the last example we have for option buttons and check boxes controls. I hope you enjoyed working with the preceding examples and having fun coding and debugging your programs.



Java Lecture Pages
 

Similar threads


Top Bottom