Java Lecture Chapter 7

Marvic

Member
Other Java Controls
In this chapter, we will learn how to use some other important controls of Java programming language. The controls we will cover in this chapter are rarely used in real-world application, thus they are not as popular as those controls that we have already covered. Nonetheless, we will learn how to use them so that when the need arises, we are already equip with the skills required to accomplish the task.

Using Scroll Bars
There are two types of scroll bars that we can apply into our program. The first one is the horizontal scroll bar and the other one is the vertical scroll bar. A scroll bar is typically used to increase or decrease a value such as when we want to change the color setting of our computer’s monitor through the Control Panel’s Display object or to increase or decrease the volume of our computer’s digital speaker. The scroll bar acts as a sliding scale with a starting point and ending point, including the values in between. Let us explore this unpopular control in our example.

Example 1:
Design and develop a Java program that demonstrates how to use a horizontal scroll bar. Follow the given design specification below:

Figure 7.1 An applet with a horintal scroll bar
Solution:

1. At the Microsoft NotePad, write the following code:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class hscroll1 extends Applet implements AdjustmentListener {
TextField txtTbox1;
Scrollbar sclHscroll1;
public void init() {
txtTbox1 = new TextField(21);
add(txtTbox1);
sclHscroll1= new Scrollbar(Scrollbar.HORIZONTAL,1,10,1,150);
add(sclHscroll1);
sclHscroll1.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent objEvent) {
if (objEvent.getAdjustable() == sclHscroll1) {
sclHscroll1.setValue(sclHscroll1.getValue());
txtTbox1.setText("Horizontal position:"+ sclHscroll1.getValue());
}
}
}

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


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

Explanation:

We are declaring the scroll bar using Scrollbar class:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class hscroll1 extends Applet implements AdjustmentListener {
TextField txtTbox1;
Scrollbar sclHscroll1;

In the init( ) method, we will add scroll bar and text field to the applet:

public void init( ) {
txtTbox1 = new TextField(21);
add(txtTbox1);
sclHscroll1= new Scrollbar(Scrollbar.HORIZONTAL,1,10,1,150);
add(sclHscroll1);
sclHscroll1.addAdjustmentListener(this);
}

The parameters of the Scrollbar method specify the initial value, the location of the scroll box in the scroll bar - called the thumb, the size of the scroll thumb in pixels,and the scroll bar’s minimum possible value which in this example is 1 and its maximum possible value is 150.

public void adjustmentValueChanged(AdjustmentEvent objEvent) {
if (objEvent.getAdjustable() == sclHscroll1) {
sclHscroll1.setValue(sclHscroll1.getValue( ));
txtTbox1.setText("Horizontal position:"+ sclHscroll1.getValue( ));
}
}
}
We can determine which scroll bar caused the event by applying the getAdjustable( ) method of the AdjustableEvent class.
To set the location of the thumb , we apply the setValue( ) method. And to get its current value, we apply the getValue( ) method. We concatenate the strings in Java programming language using the + (plus) operator. Thus, we apply it here in the txtTbox1.setText object; our last line of the code above.

Example 2:
Design and develop a Java program that demonstrates how to use a vertical scroll bar. Follow the given design specification below:

Solution:

1. At the Microsoft NotePad, write the following code:
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class vscroll1 extends Applet implements AdjustmentListener {
TextField txtTbox1;
Scrollbar sclVscroll1;
public void init() {
txtTbox1 = new TextField(28);
add(txtTbox1);
sclVscroll1= new Scrollbar(Scrollbar.VERTICAL,1,0,1,50);
add(sclVscroll1);
sclVscroll1.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent objEvent) {
if (objEvent.getAdjustable() == sclVscroll1) {
sclVscroll1.setValue(sclVscroll1.getValue());
txtTbox1.setText("Vertical location:"+ sclVscroll1.getValue());
} } }

2. Then save the Java program with the filename: vscroll1.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:

<html>
<!- Web page written with Java Applet>
<head>
<title>vscroll1</title>
</head>
<body>
<hr>
<applet
code=vscroll1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

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


Explanation:
We are declaring the scroll bar using Scrollbar class:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class vscroll1 extends Applet implements AdjustmentListener {
TextField txtTbox1;
Scrollbar sclVscroll1;

In the init( ) method, we will add scroll bar and text field to the applet:

public void init( ) {
txtTbox1 = new TextField(28);
add(txtTbox1);
sclVscroll1= new Scrollbar(Scrollbar.VERTICAL,1,0,1,50);
add(sclVscroll1);
sclVscroll1.addAdjustmentListener(this);
}

The parameters of the Scrollbar method specify the initial value, the location of the scroll box in the scroll bar - called the thumb, the size of the scroll thumb in pixels,and the scroll bar’s minimum possible value which in this example is 1 and its maximum possible value is 50.

public void adjustmentValueChanged(AdjustmentEvent objEvent) {
if (objEvent.getAdjustable( ) == sclVscroll1) {
sclVscroll1.setValue(sclVscroll1.getValue( ));
txtTbox1.setText("Vertical location:"+ sclVscroll1.getValue( ));
}
}
}

We can determine which scroll bar caused the event by applying the getAdjustable( ) method of the AdjustableEvent class.

To set the location of the thumb , we apply the setValue( ) method. And to get its current value, we apply the getValue( ) method. We concatenate the strings in Java programming language using the + (plus) operator. Thus, we apply it here in the txtTbox1.setText object; our last line of the code above.

Example 3:
Design and develop a Java program that demonstrates how to use a scrollpane. Follow the given design specification below:


Solution:

1. At the Microsoft NotePad, write the following code:
import java.applet.Applet;
import java.awt.*;
public class scrollpane1 extends Applet {
ScrollPane sclSpane1;
TextField txtTbox1;
public void init() {
sclSpane1 = new ScrollPane();
txtTbox1 = new TextField("After Bianca got a boyfriend,
I love Dimple, instead!");
sclSpane1.add(txtTbox1);
add(sclSpane1);
}
}

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

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

Explanation:
The scrollpane will enable us to place a control in the middle of a container object and display it. The scroll bars will simply appear around the edges of this container as needed. We are declaring the scrollpane using ScrollPane class:
import java.applet.Applet;
import java.awt.*;
public class scrollpane1 extends Applet {
ScrollPane sclSpane1;
TextField txtTbox1;

We will create a new control and add it to our ScrollPane object. This control is a new text field which we will add into the ScrollPane object such as the following:

public void init( ) {
sclSpane1 = new ScrollPane( );
txtTbox1 = new TextField("After Bianca got a boyfriend,
I love Dimple, instead!");
sclSpane1.add(txtTbox1);
add(sclSpane1);
}
}

We are able to accomplish the task of developing a scrollpane right at the init( ) method.

Using the GridBagLayout Manager

The GridBagLayout manager enables us to specify the position of our controls more exactly than any other layout manager we have learned. Like for example, using the GridBagLayout manager, we can design and develop an applet that provides the user with a list of names. We can display the names in command buttons at the top of the applet. When the user clicks a button, we can display the corresponding characteristic in a text area. Let us have our example for this unusual control.

Example 4:
Design and develop a Java program that demonstrates how to use a GridBagLayout manager. Follow the given design specification below:

Solution:

1. At the Microsoft NotePad, write the following code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class gridbag1 extends Applet implements ActionListener {
Button cmdTara, cmdBianca, cmdJaque, cmdDimple;
TextArea txtTarea1;
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);

constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
cmdTara = new Button("Tara");
gridbag.setConstraints(cmdTara,constraints);
add(cmdTara);
cmdTara.addActionListener(this);

constraints.weightx = 1;
cmdBianca = new Button("Bianca");
gridbag.setConstraints(cmdBianca,constraints);
add(cmdBianca);
cmdBianca.addActionListener(this);

constraints.weightx = 1;
cmdJaque = new Button("Jaque");
gridbag.setConstraints(cmdJaque,constraints);
add(cmdJaque);
cmdJaque.addActionListener(this);

constraints.weightx = 1;
cmdDimple = new Button("Dimple");
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(cmdDimple,constraints);
add(cmdDimple);
cmdDimple.addActionListener(this);

txtTarea1= new TextArea();
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(txtTarea1,constraints);
add(txtTarea1);
}
public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource() == cmdTara) {
txtTarea1.setText("Beauty Queen and Class President");
}
if (objEvent.getSource() == cmdBianca) {
txtTarea1.setText("Muse and Model");
}
if (objEvent.getSource() == cmdJaque) {
txtTarea1.setText("Body Beautiful and Cute");
}
if (objEvent.getSource() == cmdDimple) {
txtTarea1.setText("Model, Muse and First Honor");
}
}
}

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

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

Explanation:

We add the controls we need - command buttons cmdTara, cmdBianca, cmdJacque, and cmdDimple. Then we add also the text area which we named txtTarea1:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class gridbag1 extends Applet implements ActionListener {
Button cmdTara, cmdBianca, cmdJaque, cmdDimple;
TextArea txtTarea1;

Let us now set up our new GridBagLayout manager in the init( ) method. We can specify how we want our controls to be arranged under the GridBagLayout manager using an object of the Java class GridBagConstraints. We use this object to arrange the controls in a GridBagLayout:

public void init( ) {
GridBagLayout gridbag = new GridBagLayout( );
GridBagConstraints constraints = new GridBagConstraints( );
setLayout(gridbag);

constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
cmdTara = new Button("Tara");
gridbag.setConstraints(cmdTara,constraints);
add(cmdTara);
cmdTara.addActionListener(this);

constraints.weightx = 1;
cmdBianca = new Button("Bianca");
gridbag.setConstraints(cmdBianca,constraints);
add(cmdBianca);
cmdBianca.addActionListener(this);

constraints.weightx = 1;
cmdJaque = new Button("Jaque");
gridbag.setConstraints(cmdJaque,constraints);
add(cmdJaque);
cmdJaque.addActionListener(this);

constraints.weightx = 1;
cmdDimple = new Button("Dimple");
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(cmdDimple,constraints);
add(cmdDimple);
cmdDimple.addActionListener(this);

txtTarea1= new TextArea( );
constraints.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(txtTarea1,constraints);
add(txtTarea1);
}

The GridBagLayout works with the relative weights of controls in the x and y axis directions. Like for example, since all of these command buttons have the same width, they each have the same x weight which we specify as 1. We can specify x and y weights with the weightx and weighty members of the GridBagConstraints object we named constraints. Since all of our command buttons have the same height, they have the same value of weighty which we set also as 1.
Now to display the correct characteristic of the name when the user clicks a command button in our applet, we implement the actionPerformed( ) method with the conditional if statement application as follows:.
public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource( ) == cmdTara) {
txtTarea1.setText("Beauty Queen and Class President");
}
if (objEvent.getSource( ) == cmdBianca) {
txtTarea1.setText("Muse and Model");
}
if (objEvent.getSource( ) == cmdJaque) {
txtTarea1.setText("Body Beautiful and Cute");
}
if (objEvent.getSource( ) == cmdDimple) {
txtTarea1.setText("Model, Muse and First Honor");
}
}
}

Our discussion now is complete. Try clicking some of the command buttons now. And you will see that the name’s corresponding characteristic will appear. Wanna beat?



Java Lecture Pages
 

Similar threads


Latest posts

Top Bottom