Explain DataOutput interface ?
Chapter 7 EVENT HANDLING
4. Check Box Group
A check box group is a set of check boxes. In this one and only one check box from the group can be checked at one time.
These check box group are often called radio buttons.
For this type of check box you must first define the group to which they will belong and then specify that group when you construct the check boxes. Check box group are objects of type CheckboxGroup.
The check box group defines only one constructor that is: CheckboxGroup()
The methods by which other objects register and unregister to receive these events are as follows:
Void addItemListener(ItemListener il) Void removeItemListener(ItemListener il) Here, il is the item listener.
import java.applet.*; import java.awt.*; import java.awt.event.*; /*
<applet code = "awt4" width = 400 height = 400> </applet>
*/
public class awt4 extends Applet implements ItemListener {
Label l1;
public void init() {
CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("Pink", cbg, true); cb1.addItemListener(this);
add(cb1);
Checkbox cb2=new Checkbox("Blue", cbg, true); cb2.addItemListener(this);
add(cb2);
Checkbox cb3=new Checkbox("Black", cbg, true); cb3.addItemListener(this);
add(cb3);
Checkbox cb4=new Checkbox("White", cbg, true); cb4.addItemListener(this);
add(cb4);
l1=new Label(" "); add(l1);
}
public void itemStateChanged(ItemEvent ae) { Checkbox cb=(Checkbox)ae.getItemSelectable(); l1.setText(cb.getLabel()); } } 5. Choice
A choice is a component that provides a list of menu items.
When user clicks on a choice, it displays whole list of choice and then a selection can be made.
It can have only one item selected at a time. Choice shows the currently selected item. Choice defines only one constructor that is:
Choice()
An item event is generated when a user selects one of the entries in a choice.
The methods by which other objects register and unregister to receive these events are as follows:
Void addItemListener(ItemListener il) Void removeItemListener(ItemListener il) Here, il is the item listener.
Example
import java.applet.*; import java.awt.*; import java.awt.event.*; /*
<applet code = "choice1" width = 400 height = 400> </applet>
*/
public class choice1 extends Applet implements ItemListener {
public void init() {
Choice c1 = new Choice(); c1.addItem("Pink"); c1.addItem("Blue"); c1.addItem("Black"); c1.addItem("White"); c1.addItemListener(this); add(c1); l1=new Label(" "); add(l1); }
public void itemStateChanged(ItemEvent ae) { Choice cb=(Choice)ae.getItemSelectable(); l1.setText(cb.getSelectedItem()); } } 6. List
A list is a component that allows a user to select one or more items.
If the list is large then it will automatically provide scroll bars so that user may see the entire list.
An action event is generated when a user double-clicks on an item in a list.
An item event is generated when a user selects or deselects one of the entries.
The List defines these constructors: List()
List(int rows)
List(int rows, Boolean multiple)
Here in second form rows is the number of items that are visible to a user. In third form if multiple is true, a user can select multiple entries from a list. Otherwise, only one item can be selected.
The methods by which other objects register and unregister to receive these events are as follows:
Void addItemListener(ItemListener il) Void removeItemListener(ItemListener il) Here, il is the item listener.
The methods by which other objects register and unregister to receive action events generated by this component are:
Void addActionListener(ActionListener al) Void removeActionListener(ActionListener al) Here, al is an action listener.
7. Scrollbar
Scrollbars are used to select any integer values between a specified minimum and maximum.
A scroll bar contains a slider that can be dragged to continuously vary its value.
Alternatively, the user can click on one of the buttons at either end of the scroll bar or in the area between the slider and the buttons.
These operations also modify its value. The Scrollbar class defines these constructors:
Scrollbars()
Scrollbar(int orientation)
Scrollbar(int orientation, int value, int width, int min, int max)
Scrollbars can be horizontally or vertically oriented. It is assigned the value HORIZONTAL or VERTICAL.
The argument value represents the initial value of the scroll bar. The width of the slider is width.
The range of the scroll bar is determined by the min and max arguments. import java.applet.*;
import java.awt.*; import java.awt.event.*; /*
<applet code = "scroll1" width = 400 height = 400> </applet>
*/
public class scroll1 extends Applet implements AdjustmentListener {
TextArea ta; public void init() {
Scrollbar sb1 = new Scrollbar(Scrollbar.VERTICAL,50,25,0,100); sb1.addAdjustmentListener(this);
add(sb1);
ta = new TextArea(10, 20); add(ta);
}
public void adjustmentValueChanged(AdjustmentEvent ie) {
Scrollbar sb1 = (Scrollbar)ie.getAdjustable();
ta.append("AdjustEvent: " + sb1.getValue() + "\n"); }
8. TextField
A textfield allows a user to enter one line of text.
Textfield allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse selections.
Textfield defines the following constructors: TextField()
TextField(String str) TextField(int cols)
TextField(String str, int cols)
Here, str is the text for the field. The argument cols indicates the width of the field in character.
If we don‟t want to display characters use setEchoChar() method. Example import java.applet.*; import java.awt.*; import java.awt.event.*; /*
<applet code = "textfield1" width = 400 height = 400> </applet>
*/
public class textfield1 extends Applet {
TextField name, pass; public void init() {
Label lnm = new Label("Name : "); name = new TextField(12);
add(lnm); add(name);
Label lpass = new Label("Password : "); pass = new TextField(8);
pass.setEchoChar('='); add(lnm); add(name); add(lpass); add(pass); } } 9. TextArea
A text area allows a user to enter multiple lines of text.
To handle these situations, the AWT includes a simple multiline editior called TextArea. TextArea defines the following constructors:
TextArea()
TextArea(String str)
TextArea(int rows, int cols)
TextArea(String str, int rows, int cols)
TextArea(String str, int rows, int cols, int scrollbars)
Here, str is the text for the area. The rows and cols arguments indicate the number of rows and columns in the component.
The scrollbars argument indicates if horizontal and/or vertical scroll bars should be created. Its possible values are:
SCROLLBARS_BOTH, SCROLLBARS_HORIZONTAL_ONLY, SCROLLBARS_VERTICAL_ONLY SCROLLBARS_NONE. Example import java.applet.*; import java.awt.*; import java.awt.event.*; /*
<applet code = "textarea1" width = 400 height = 400> </applet>
*/
public class textarea1 extends Applet {
public void init() {
String txt = "A text area allows a user a user to enter \n " + "multiple lines of text. Sometimes a single line \n" +
"of text input is not enough for a given task. \n" + "To handle these situations, the AWT includes a \n" + "simple multiline editor called TextArea. ";
TextArea ta = new TextArea(txt, 10, 30); add(ta);
} }
10. Canvas
A canvas provides a rectangular area on which we can draw.
This is valuable because we can create graphs and other diagrams on the canvas by using methods of the Graphics class.
The Canvas class extends Components. Canvas defines this constructor: Canvas()
One need to override the paint() method to draw on the canvas.
Example
import java.applet.*; import java.awt.*; /*
<applet code = "CanvasDemo" width = 400 height = 400> </applet>
*/
class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.red); g.fillRect(10,10,100,50); }
}
public class CanvasDemo extends Applet {
public void init() {
MyCanvas myc = new MyCanvas(); myc.setSize(200,200);
add(myc); }
}