Java Lecture Chapter 4

Marvic

Member
Arranging and Organizing Controls

The GridLayout Manager
By far, we were just using the Flow Layout Manager in all our previous examples, whether we like it or not. Because in Java programming, by default, this Flow Layout Manager is utilized by the Java compiler once we put the controls in our applet. To explain it further, this Flow Layout manager simply places the controls in an applet one by one, wrapping them at the end of a row; while in the GridLayout Manager, it places the controls in a grid to arrange the controls vertically and add them to our applet. This is also the main reason why we keep on adjusting the width and height of our applet in the HTML script to accommodate the controls in order to be displayed properly. However, this is but just a quick fix in our part. In other words, with the default Flow Layout Manager, we have no other way on where to place the controls wherever we want to display them on the applet. Using the GridLayout Manager, we can place the control to any parts of the applet, though we need to add some laborious code in our Java program. Well, it’s worth the effort.

Example 1:
Using the GridLayout Manager, design and develop a Java program that multiplies the two input numbers. Follow the design specification below (in a grid form):

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class multiply1 extends Applet implements ActionListener {
TextField txtNum1, txtNum2, txtProduct;
Label lblAsterisk, lblBlank1, lblBlank2,lblBlank3,lblBlank4,
 lblBlank5,lblBlank6,lblBlank7,lblBlank8,lblBlank9,lblBlank10;
Label lblSpace1, lblSpace2, lblSpace3, lblSpace4, lblSpace5,
 lblSpace6;
Button cmdCompute;
public void init() {
  setLayout( new GridLayout(9,3));
  lblBlank1 = new Label();
  add(lblBlank1);
  txtNum1 = new TextField(5);
  add(txtNum1);
  lblBlank2 = new Label();
  add(lblBlank2);
  lblBlank3 = new Label();
  add(lblBlank3);
  lblAsterisk = new Label("*",Label.CENTER);
  add(lblAsterisk);
  lblBlank4 = new Label();
  add(lblBlank4);
  lblBlank5 = new Label();
  add(lblBlank5);
  txtNum2 = new TextField(5);
  add(txtNum2);
  lblBlank6 = new Label();
  add(lblBlank6);
  lblSpace1 = new Label();
  add(lblSpace1);
  lblSpace2 = new Label();
  add(lblSpace2);
  lblSpace3 = new Label();
  add(lblSpace3);
  lblBlank7 = new Label();
  add(lblBlank7);
  cmdCompute = new Button("Compute");
  add(cmdCompute);
  cmdCompute.addActionListener(this);
  lblBlank8 = new Label();
  add(lblBlank8);
  lblSpace4 = new Label();
  add(lblSpace4);
  lblSpace5 = new Label();
  add(lblSpace5);
  lblSpace6 = new Label();
  add(lblSpace6);
  lblBlank9 = new Label();
  add(lblBlank9);
  txtProduct = new TextField(8);
  add(txtProduct);
  lblBlank10 = new Label();
  add(lblBlank10);
}
public void actionPerformed (ActionEvent objEvent) {
int varNum1, varNum2, varProduct;
  if (objEvent.getSource()==cmdCompute) {
     varNum1 = Integer.parseInt(txtNum1.getText());
     varNum2 = Integer.parseInt(txtNum2.getText());
     varProduct = varNum1 * varNum2;
     txtProduct.setText(String.valueOf(varProduct));
   }
}
}

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

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

Explanation:
You will notice here in our code below that we declare so many labels. We need these set of labels without any text on them to act as space allocator in our applet. They will serve to fill the blank entries in our 9x3 grid.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class multiply1 extends Applet implements ActionListener {
TextField txtNum1, txtNum2, txtProduct;
Label lblAsterisk, lblBlank1, lblBlank2,lblBlank3,lblBlank4,
 lblBlank5,lblBlank6,lblBlank7,lblBlank8,lblBlank9,lblBlank10;
Label lblSpace1, lblSpace2, lblSpace3, lblSpace4, lblSpace5,
 lblSpace6;
Button cmdCompute;

This time, we need to install the GridLayout manager in our applet and add the controls on it, by setting it up with the setLayOut( ) method. We can accomplish this task in the init( ) method as shown below:
Code:
public void init( ) {
setLayout( new GridLayout(9,3));
  lblBlank1 = new Label( );
  add(lblBlank1);
  txtNum1 = new TextField(5);
  add(txtNum1);
  lblBlank2 = new Label( );
  add(lblBlank2);
  lblBlank3 = new Label( );
  add(lblBlank3);
  lblAsterisk = new Label("*",Label.CENTER);
  add(lblAsterisk);
  lblBlank4 = new Label( );
  add(lblBlank4);
  lblBlank5 = new Label( );
  add(lblBlank5);
  txtNum2 = new TextField(5);
  add(txtNum2);
  lblBlank6 = new Label( );
  add(lblBlank6);
  lblSpace1 = new Label();
  add(lblSpace1);
  lblSpace2 = new Label( );
  add(lblSpace2);
  lblSpace3 = new Label( );
  add(lblSpace3);
  lblBlank7 = new Label( );
  add(lblBlank7);
  cmdCompute = new Button("Compute");
  add(cmdCompute);
  cmdCompute.addActionListener(this);
  lblBlank8 = new Label( );
  add(lblBlank8);
  lblSpace4 = new Label( );
  add(lblSpace4);
  lblSpace5 = new Label( );
  add(lblSpace5);
  lblSpace6 = new Label( );
  add(lblSpace6);
  lblBlank9 = new Label( );
  add(lblBlank9);
  txtProduct = new TextField(8);
  add(txtProduct);
  lblBlank10 = new Label();
  add(lblBlank10);
}

Take note also that to place the asterisk symbol in the middle of its label, we include the class constant Label.CENTER in the call to the constructor of the Label class, with the following syntax:

lblAsterisk = new Label("*",Label.CENTER);

We can make the program to compute the product of two input numbers by implementing the ActionListener interface, and connect our command button (cmdCompute) to it. Then finally add the actionPerformed ( ) method to complete the process:
Code:
public void actionPerformed (ActionEvent objEvent) {
int varNum1, varNum2, varProduct;
  if (objEvent.getSource()==cmdCompute) {
     varNum1 = Integer.parseInt(txtNum1.getText( ));
     varNum2 = Integer.parseInt(txtNum2.getText( ));
     varProduct = varNum1 * varNum2;
     txtProduct.setText(String.valueOf(varProduct));
   }
}
}

When the command button (cmdCompute) is clicked, we instruct the computer to read the numeric data entered by the user in the two text fields, txtNum1 and txtNum2. To read the numeric data from the text field, we apply the getText( ) method such as the following syntax:

txtNum1.getText( )

This syntax returns the text string in the text field, txtNum1. For example, if the user inputs the data “5” in txtNum1 text field, this is actually a text string value. We need to convert it to its equivalent numeric value using the parseInt( ) method under the Integer class of the Java language such as the following syntax:

Integer.parseInt(txtNum1.getText( ))

In this way, we can now multiply the two numeric data entered by the user from text field 1 txtNum1 and text field txtNum2 and storing the result to variable varProduct. Now we need to convert back the numeric value stored in the varProduct into its equivalent text string so that we can display it to the text field, txtProduct using the valueOf( ) method under the String class of the Java language. Here is its command syntax below:
txtProduct..setText(String.valueOf(varProduct))

Example 2:
Using the GridLayout Manager, design and develop a Java program that multiplies the two input numbers. This time, we have to put label messages “1st No.” on Blank1, “2nd No.” on Blank5, and “Answer:” on Blank9. Follow the design specification below (in a grid form):

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class multiply2 extends Applet implements ActionListener {
TextField txtNum1, txtNum2, txtProduct;
Label lblAsterisk, lblBlank1, lblBlank2,lblBlank3,lblBlank4,
 lblBlank5,lblBlank6,lblBlank7,lblBlank8,lblBlank9,lblBlank10;
Label lblSpace1, lblSpace2, lblSpace3, lblSpace4, lblSpace5,
 lblSpace6;
Button cmdCompute;
public void init() {
  setLayout( new GridLayout(9,3));
  lblBlank1 = new Label("1st No.");
  add(lblBlank1);
  txtNum1 = new TextField(5);
  add(txtNum1);
  lblBlank2 = new Label();
  add(lblBlank2);
  lblBlank3 = new Label();
  add(lblBlank3);
  lblAsterisk = new Label("*",Label.CENTER);
  add(lblAsterisk);
  lblBlank4 = new Label();
  add(lblBlank4);
  lblBlank5 = new Label("2nd No.");
  add(lblBlank5);
  txtNum2 = new TextField(5);
  add(txtNum2);
  lblBlank6 = new Label();
  add(lblBlank6);
  lblSpace1 = new Label();
  add(lblSpace1);
  lblSpace2 = new Label();
  add(lblSpace2);
  lblSpace3 = new Label();
  add(lblSpace3);
  lblBlank7 = new Label();
  add(lblBlank7);
  cmdCompute = new Button("Compute");
  add(cmdCompute);
  cmdCompute.addActionListener(this);
  lblBlank8 = new Label();
  add(lblBlank8);
  lblSpace4 = new Label();
  add(lblSpace4);
  lblSpace5 = new Label();
  add(lblSpace5);
  lblSpace6 = new Label();
  add(lblSpace6);
  lblBlank9 = new Label("Answer:");
  add(lblBlank9);
  txtProduct = new TextField(8);
  add(txtProduct);
  lblBlank10 = new Label();
  add(lblBlank10);
}
public void actionPerformed (ActionEvent objEvent) {
int varNum1, varNum2, varProduct;
  if (objEvent.getSource()==cmdCompute) {
     varNum1 = Integer.parseInt(txtNum1.getText());
     varNum2 = Integer.parseInt(txtNum2.getText());
     varProduct = varNum1 * varNum2;
     txtProduct.setText(String.valueOf(varProduct));
   }
}
}

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

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

Explanation:
You will notice here in our code below that we declare so many labels. We need these set of labels without any text on them to act as space allocator in our applet. They will serve to fill the blank entries in our 9x3 grid.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class multiply2 extends Applet implements ActionListener {
TextField txtNum1, txtNum2, txtProduct;
Label lblAsterisk, lblBlank1, lblBlank2,lblBlank3,lblBlank4,
 lblBlank5,lblBlank6,lblBlank7,lblBlank8,lblBlank9,lblBlank10;
Label lblSpace1, lblSpace2, lblSpace3, lblSpace4, lblSpace5,
 lblSpace6;
Button cmdCompute;

This time, we need to install the GridLayout manager in our applet and add the controls on it, by setting it up with the setLayOut( ) method. We can accomplish this task in the init( ) method as shown below:
Code:
public void init( ) {
  setLayout( new GridLayout(9,3));
  lblBlank1 = new Label("1st No.");
  add(lblBlank1);
  txtNum1 = new TextField(5);
  add(txtNum1);
  lblBlank2 = new Label( );
  add(lblBlank2);
  lblBlank3 = new Label( );
  add(lblBlank3);
  lblAsterisk = new Label("*",Label.CENTER);
  add(lblAsterisk);
  lblBlank4 = new Label( );
  add(lblBlank4);
  lblBlank5 = new Label("2nd No.");
  add(lblBlank5);
  txtNum2 = new TextField(5);
  add(txtNum2);
  lblBlank6 = new Label( );
  add(lblBlank6);
  lblSpace1 = new Label( );
  add(lblSpace1);
  lblSpace2 = new Label( );
  add(lblSpace2);
  lblSpace3 = new Label( );
  add(lblSpace3);
  lblBlank7 = new Label( );
  add(lblBlank7);
  cmdCompute = new Button("Compute");
  add(cmdCompute);
  cmdCompute.addActionListener(this);
  lblBlank8 = new Label( );
  add(lblBlank8);
  lblSpace4 = new Label( );
  add(lblSpace4);
  lblSpace5 = new Label( );
  add(lblSpace5);
  lblSpace6 = new Label( );
  add(lblSpace6);
  lblBlank9 = new Label("Answer:");
  add(lblBlank9);
  txtProduct = new TextField(8);
  add(txtProduct);
  lblBlank10 = new Label( );
  add(lblBlank10);
}

We simply put label messages “1st No.” on Blank1, “2nd No.” on Blank5, and “Answer:” on Blank9, as you can observe in our code above.

We can make the program to compute the product of two input numbers by implementing the ActionListener interface, and connect our command button (cmdCompute) to it. Then finally add the actionPerformed ( ) method to complete the process:
Code:
public void actionPerformed (ActionEvent objEvent) {
int varNum1, varNum2, varProduct;
  if (objEvent.getSource()==cmdCompute) {
     varNum1 = Integer.parseInt(txtNum1.getText( ));
     varNum2 = Integer.parseInt(txtNum2.getText( ));
     varProduct = varNum1 * varNum2;
     txtProduct.setText(String.valueOf(varProduct));
   }
}
}
When the command button (cmdCompute) is clicked, we instruct the computer to read the numeric data entered by the user in the two text fields, txtNum1 and txtNum2. To read the numeric data from the text field, we apply the getText( ) method such as the following syntax:

txtNum1.getText( )

This syntax returns the text string in the text field, txtNum1. For example, if the user inputs the data “5” in txtNum1 text field, this is actually a text string value. We need to convert it to its equivalent numeric value using the parseInt( ) method under the Integer class of the Java language such as the following syntax:

Integer.parseInt(txtNum1.getText( ))

In this way, we can now multiply the two numeric data entered by the user from text field 1 txtNum1 and text field txtNum2 and storing the result to variable varProduct. Now we need to convert back the numeric value stored in the varProduct into its equivalent text string so that we can display it to the text field, txtProduct using the valueOf( ) method under the String class of the Java language. Here is its command syntax below:
txtProduct..setText(String.valueOf(varProduct))

Example 3:
Using the GridLayout Manager, Design and develop a Java program that converts the input value of Celsius into its equivalent Fahrenheit degree. Use the formula: F = (9/5) * C + 32. Then display the result. Follow the design specification below (in a grid form):

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class degree2 extends Applet implements ActionListener {
TextField txtCelsius, txtFahrenheit;
Label lblCelsius, lblFahrenheit;
Label lblBlank1, lblBlank2,lblBlank3,lblBlank4;
Label lblSpace1,lblSpace2, lblSpace3, lblSpace4, lblSpace5,
      lblSpace6;
Button cmdConvert;
public void init() {
  setLayout( new GridLayout(7,3));
  lblCelsius = new Label("Enter Celsius:");
  add(lblCelsius);
  txtCelsius = new TextField(5);
  add(txtCelsius);
  lblBlank1 = new Label();
  add(lblBlank1);
  lblSpace1 = new Label();
  add(lblSpace1);
  lblSpace2 = new Label();
  add(lblSpace2);
  lblSpace3 = new Label();
  add(lblSpace3);
  lblBlank2 = new Label();
  add(lblBlank2);
  cmdConvert = new Button("Convert");
  add(cmdConvert);
  cmdConvert.addActionListener(this);
  lblBlank3 = new Label();
  add(lblBlank3);
  lblSpace4 = new Label();
  add(lblSpace4);
  lblSpace5 = new Label();
  add(lblSpace5);
  lblSpace6 = new Label();
  add(lblSpace6);
  lblFahrenheit = new Label("Fahrenheit:");
  add(lblFahrenheit);
  txtFahrenheit = new TextField(8);
  add(txtFahrenheit);
  lblBlank4 = new Label();
  add(lblBlank4);
}
public void actionPerformed (ActionEvent objEvent) {
int varCelsius;
double varFahrenheit;
  if (objEvent.getSource()==cmdConvert) {
     varCelsius = Integer.parseInt(txtCelsius.getText());
     varFahrenheit = (9.0/5.0)* (varCelsius + 32.0);
     txtFahrenheit.setText(String.valueOf(varFahrenheit));
   }
}
}

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

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

Explanation:
You will notice here in our code below that we declare so many labels. We need these set of labels without any text on them to act as space allocator in our applet. They will serve to fill the blank entries in our 7x3 grid.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class degree2 extends Applet implements ActionListener {
TextField txtCelsius, txtFahrenheit;
Label lblCelsius, lblFahrenheit;
Label lblBlank1, lblBlank2,lblBlank3,lblBlank4;
Label lblSpace1,lblSpace2, lblSpace3, lblSpace4, lblSpace5,
      lblSpace6;
Button cmdConvert;

This time, we need to install the GridLayout manager in our applet and add the controls on it, by setting it up with the setLayOut( ) method. We can accomplish this task in the init( ) method as shown below:
Code:
public void init( ) {
  setLayout( new GridLayout(7,3));
  lblCelsius = new Label("Enter Celsius:");
  add(lblCelsius);
  txtCelsius = new TextField(5);
  add(txtCelsius);
  lblBlank1 = new Label( );
  add(lblBlank1);
  lblSpace1 = new Label( );
  add(lblSpace1);
  lblSpace2 = new Label( );
  add(lblSpace2);
  lblSpace3 = new Label( );
  add(lblSpace3);
  lblBlank2 = new Label( );
  add(lblBlank2);
  cmdConvert = new Button("Convert");
  add(cmdConvert);
  cmdConvert.addActionListener(this);
  lblBlank3 = new Label( );
  add(lblBlank3);
  lblSpace4 = new Label( );
  add(lblSpace4);
  lblSpace5 = new Label( );
  add(lblSpace5);
  lblSpace6 = new Label( );
  add(lblSpace6);
  lblFahrenheit = new Label("Fahrenheit:");
  add(lblFahrenheit);
  txtFahrenheit = new TextField(8);
  add(txtFahrenheit);
  lblBlank4 = new Label( );
  add(lblBlank4);
}

We can make the program to convert the input Celsius degree into its equivalent Fahrenheit degree by implementing the ActionListener interface, and connect our command button (cmdConvert) to it. Then finally add the actionPerformed ( ) method to complete the process:
Code:
public void actionPerformed (ActionEvent objEvent) {
int varCelsius;
double varFahrenheit;
  if (objEvent.getSource( )==cmdConvert) {
     varCelsius = Integer.parseInt(txtCelsius.getText( ));
     varFahrenheit = (9.0/5.0)* (varCelsius + 32.0);
     txtFahrenheit.setText(String.valueOf(varFahrenheit));
   }
}
}

When the command button (cmdConvert) is clicked, we instruct the computer to read the numeric data entered by the user in text field 1, txtCelsius. To read the numeric data from the text field, we apply the getText( ) method such as the following syntax:

txtCelsius.getText( )

This syntax returns the text string in the text field, txtCelsius. For example, if the user inputs the data “5” in txtCelsius text field, this is actually a text string value. We need to convert it to its equivalent numeric value using the parseInt( ) method under the Integer class of the Java language such as the following syntax:

Integer.parseInt(txtCelsius.getText( ))

In this way, we can now store the entered numeric data from txtCelsius and store the result to variable varCelsius. This time, we can use the numeric data from varCelsius for our equation to get the convertion into the Fahrenheit degree with the following syntax:

varFahrenheit = (9.0/5.0)* (varCelsius + 32.0);

Now we need to convert back the numeric value stored in the varFahrenheit into its equivalent text string so that we can display it to the text field, txtFahrenheit using the valueOf( ) method under the String class of the Java language. Here is its command syntax below:

txtProduct.setText(String.valueOf(varProduct))

Now this time, I’m sure we are pretty well knowledgeable on how to use the GridLayout Manager to arrange the displayed controls in our applet. With this skill, we can continue learning more on how to arrange the controls using the Panels. This is our topic for the next example. So be ready now!

The Panel Class
Another way of arranging controls is using the Panel class. A panel is a rectangular region that contains a group of control such as check boxes or option buttons. Let us have now the examples to learn it in action.

Example 4:
Using a Panel, design and develop a Java program that contains two panels for two groups of 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.*;
public class checkpanel1 extends Applet {
Checkboxpanel panPanel1, panPanel2;
public void init() {
   setLayout(new GridLayout(1,2));
   panPanel1 = new Checkboxpanel();
   panPanel2 = new Checkboxpanel();
   add(panPanel1);
   add(panPanel2);
   }
}
class Checkboxpanel extends Panel {
Checkbox chkBox1, chkBox2, chkBox3;
Checkboxpanel() {
  chkBox1 = new Checkbox("Check 1");
  add(chkBox1);
  chkBox2 = new Checkbox("Check 2");
  add(chkBox2);
  chkBox3 = new Checkbox("Check 3");
  add(chkBox3);
}
}

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

4. Now save the HTML script with the filename: checkpanel1.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 checkpanel1.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\checkpanel1.htm
Explanation:
First, we have to create the check box panel by deriving it from the Java language Panel class as shown in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
public class checkpanel1 extends Applet {
Checkboxpanel panPanel1, panPanel2;

Since we need two panels for our two groups of checkboxes, that is why we declared the two panels which we named: panPanel1, and panPanel2.

Next, we have to initialize these two panels in the init( ) method. We will apply the GridLayout manager with 1 row and 2 columns to make these panels appear side by side, as you can see in the following code:
Code:
public void init( ) {
   setLayout(new GridLayout(1,2));
   panPanel1 = new Checkboxpanel( );
   panPanel2 = new Checkboxpanel( );
   add(panPanel1);
   add(panPanel2);
   }
}
/[CODE]

Lastly, we have to create and add the new check boxes to this panel. We can accomplish this task in the constructor method of the Panel class:
[CODE]
class Checkboxpanel extends Panel {
Checkbox chkBox1, chkBox2, chkBox3;
Checkboxpanel( ) {
  chkBox1 = new Checkbox("Check 1");
  add(chkBox1);
  chkBox2 = new Checkbox("Check 2");
  add(chkBox2);
  chkBox3 = new Checkbox("Check 3");
  add(chkBox3);
}
}

Unlike in our previous examples, we always create and add the new check boxes in the init( ) method . In the Panel class, creating and adding the check boxes in the init( ) method is not supported. The constructor method has the same name with its associated class and it is automatically run when an object of the class is created.

Example 5:
Using a Panel, design and develop a Java program that contains two panels for two groups of option buttons. Follow the design specification below:

Solution:
1. At the Microsoft NotePad, write the following code:
Code:
import java.applet.Applet;
import java.awt.*;
 public class optionpanel1 extends Applet {
 Optionbuttonpanel panPanel1, panPanel2;
 public void init() {
      setLayout(new GridLayout(1,2));
      panPanel1 = new Optionbuttonpanel();
      panPanel2 = new Optionbuttonpanel();
      add(panPanel1);
      add(panPanel2);
 }
}
class Optionbuttonpanel extends Panel {
   CheckboxGroup optGroup;
   Checkbox optButton1, optButton2, optButton3;
     Optionbuttonpanel() {
     optGroup = new CheckboxGroup();
     add(optButton1 = new Checkbox("Option 1",optGroup,false));
     add(optButton2 = new Checkbox("Option 2",optGroup,false));
     add(optButton3 = new Checkbox("Option 3",optGroup,false));
    }
}

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

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

Explanation:
First, we have to create the option button panel by deriving it from the Java language Panel class as shown in our code below:
Code:
import java.applet.Applet;
import java.awt.*;
 public class optionpanel1 extends Applet {
 Optionbuttonpanel panPanel1, panPanel2;

Since we need two panels for our two groups of option buttons, that is why we declared the two panels which we named: panPanel1, and panPanel2.
Next, we have to initialize these two panels in the init( ) method. We will apply the GridLayout manager with 1 row and 2 columns to make these panels appear side by side, as you can see in the following code:
Code:
  public void init( ) {
      setLayout(new GridLayout(1,2));
      panPanel1 = new Optionbuttonpanel( );
      panPanel2 = new Optionbuttonpanel( );
      add(panPanel1);
      add(panPanel2);
  }
}

Lastly, we have to create and add the new option buttons to this panel. We can accomplish this task in the constructor method of the Panel class:
Code:
class Optionbuttonpanel extends Panel {
   CheckboxGroup optGroup;
   Checkbox optButton1, optButton2, optButton3;
     Optionbuttonpanel( ) {
     optGroup = new CheckboxGroup( );
     add(optButton1 = new Checkbox("Option 1",optGroup,false));
     add(optButton2 = new Checkbox("Option 2",optGroup,false));
     add(optButton3 = new Checkbox("Option 3",optGroup,false));
    }
}

Unlike in our previous examples, we always create and add the new option buttons in the init( ) method. In the Panel class, creating and adding the option buttons in the init( ) method is not supported. The constructor method has the same name with its associated class and it is automatically run when an object of the class is created.



Java Lecture Pages
 

Similar threads


Top Bottom