Adding Controls

Adding Controls
* Controls are components that allow a user to interact with your application in various ways—for example, a commonly used control is the push button.
* A layout manager automatically positions components within a container. Thus, the appearance of a window is determined by a combination of the controls that it contains and the layout manager used to position them.In addition to the controls, a frame window can also include a standard-style menu bar.
* Each entry in a menu bar activates a drop-down menu of options from which the user can choose. A menu bar is always positioned at the top of a window. Although different in appearance, menu bars are handled in much the same way as are the other controls. While it is possible to manually position components within a window, doing so is quite tedious. The layout manager automates this task.
Control Fundamentals: -
The Applet supports the following types of controls:
1. Buttons: -Buttons A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button . Button defines these two constructors: Button( ) : Creates an empty button Button(String str ) : Creates a button that contains str as a label. These methods are as follows: void setLabel(String str ) : , str becomes the new label for the button String getLabel( ):GET the label name for button
The button in this applet is an object that belongs to the class Button (more properly, java.awt.Button). When the applet is created, the button must be created and added to the applet. This is part of the process of initializing the applet.
Example 1: - We have an object b that looks like a Button
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
public void init()
{ b = new Button("Anuj");
add(b);
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 2: -We include the mouseUp() where we change the label or the name of the button with the setLabel() .We have a variable i which we increment everytime the user clicks with the mouse.The increased value of i is displayed as the name of the button.
// setLabel()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
int i=0;
public void init()
{ b=new Button("i..." + i);
add(b);
}
public boolean mouseUp(Event e, int x, int y)
{ i++;
b.setLabel("i..." + i);
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >< br > < applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 3: - To change the shape and the position of the button.
// reshape()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
int i = 0;
public void init()
{ b = new Button("Salary");
add(b);
}
public boolean mouseUp(Event e, int x, int y)
{ b.reshape(x, y, 60, 50);
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 4: - To get the label of the button as well.
// getLabel()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
int i = 0;
public void init()
{ b=new Button("hello");
add(b);
}
public boolean mouseUp(Event e,int x, int y)
{ showStatus(b.getLabel());
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 5: - When the user clicks with the mouse, it is checked if the label of the button is "hello" or "hi".
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
public void init()
{ b=new Button("hello");
add(b);
}
public boolean mouseUp(Event e,int x, int y)
{ if(b.getLabel()=="hello")
b.setLabel("hi");
else
b.setLabel("hello");
return true;
}
}
/*
< html > < title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 6: - To Hide and Seek the bution
//Hide and seek
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button a, b, c;
public void init()
{ a = new Button("Hide");
b = new Button("Show");
c = new Button("Hello");
add(a);
add(b);
add(c);
}
public boolean action(Event e,Object o)
{ if("Hide".equals(o))
{ c.hide();
}
if("Show".equals(o))
{ c.show();
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html>
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Note* : - A program works exactly like this one. But instead of hide() and the show() , we have the enable() and the disable() . function.
2. TextField: - A scrollable text display object with one row of characters is known as the TextField.
TextField The TextField class implements a single-line text-entry area, usually called an edit control. Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse selections. TextField is a subclass of TextComponent . TextField defines the following constructors: TextField( ) : creates a default text field. TextField(int numChars ): creates a text field that is numChars characters wide TextField(String str ): initializes the text field with the string contained in str TextField(String str , int numChars ): initializes a text field and sets its width. To obtain the string currently contained in the text field, call getText( ) . To set the text, call setText( ) . These methods are as follows: String getText( ) void setText(String str )
Example 1: - To create a TextField 60 characters long and a button to go along with it.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ Button b;
TextField t;
public void init()
{ b = new Button("click");
t = new TextField(60);
add(b);
add(t);
}
public boolean action (Event e, Object o)
{ if ("click".equals(o))
{ showStatus(t.getText());
t.setText("Hello how are you");
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 2: - We create a text field that has 5 lines and 60 columns. In the action() we don't say setText, but appendText . This means that the text provided in this way will be added on to the text already present in the text field.
import java.applet.*;
import java.awt.*;
//Append Text
public class zzz extends Applet
{ Button b;
TextArea t;
public void init()
{ b = new Button("click");
t = new TextArea(5,60);
add(b);
add(t);
}
public boolean action (Event e, Object o)
{ if ("click".equals(o))
{ showStatus(t.getText());
t.appendText("Hello how are you");
}
return true;
}
}
/*
< html >
< title >The Hello, World Applet< /title >
< applet code="zzz.class" width="320" height="120" >
< /applet >
< /html >
*/ Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

3. CheckboxGroup: - CheckboxGroup/Radio buttons check boxes in which one and only one check box in the group can be checked at any one time. These check boxes are often called radio buttons, because they act like the station selector on a car radio—only one station can be selected at any one time. These methods are as follows: Checkbox getSelectedCheckbox( ) void setSelectedCheckbox(Checkbox which ) Here, which is the check box that you want to be selected.
Note*; - Excute this program in Netbeans.
Example: - import java.applet.Applet;
import java.awt.Checkbox;
public class CreateCheckedCheckBox extends Applet{
public void init(){
//crete checkboxes
Checkbox checkBox1 = new Checkbox("My Checkbox 1");
Checkbox checkbox2 = new Checkbox("My Checkbox 2", true);
//add checkboxes using add method
add(checkBox1);
add(checkbox2);
}
}
/*
< applet code="CreateCheckBox" width=200 height=200 >
< /applet >
*/
Output: -

4. Choice: - Choice Controls/Dropdown Menu The Choice class is used to create a pop-up list of items from which the user may choose. Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the whole list of choices pops up, and a new selection can be made. Items are added to the list in the order in which calls to add( ) occur.
Example 1: - To add item in list box.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l;
Choice c;
public void init()
{ c=new Choice();
c.addItem("xxx");
c.addItem("yyy");
c.addItem("zzz");
l = new List();
l.addItem("a1");
l.addItem("a3");
l.addItem("a2");
add(l);
add(c);
}
}
/*
< applet code="zzz.class" width=200 height=200 >
< /applet >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 2: - To delete item in list box.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l;
Button b;
public void init()
{ b = new Button("Remove");
l = new List(3, false);
l.addItem("aaa");
l.addItem("bbb");
l.addItem("ccc");
l.addItem("ddd");
l.addItem("eee");
add(l);
add(b);
}
public boolean action (Event e, Object o)
{ if("Remove".equals(o))
{ l.delItem(1);
}
return true;
}
}
/*
< applet code="zzz.class" width=200 height=200 >
< /applet >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 3: - To select Multiple Choice in list box
import java.applet.*; import java.awt.*; public class zzz extends Applet { List l; Choice c; public void init() { c.addItem("xxx"); c.addItem("yyy"); c.addItem("zzz"); l = new List(); l.addItem("a1"); l.addItem("a3"); l.addItem("a2"); add(l); add(c); } } /*
< applet code="zzz.class" width=200 height=200 >
< /applet >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 4: - To get getSelectedIndex() and getSelectedItem()
// getSelectedIndex() and getSelectedItem()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l;
int i=0;
String s;
public void init()
{ l = new List(3,false);
l.addItem("aaa");
l.addItem("bbb");
l.addItem("ccc");
l.addItem("ddd");
l.addItem("eee");
add(l);
}
public boolean handleEvent(Event e)
{ if (e.target instanceof List)
{ i=l.getSelectedIndex();
s=l.getSelectedItem();
showStatus("Name.."+s+"..no.."+i);
}
return true;
}
}
/*
< applet code="zzz.class" width=200 height=200 >
< /applet >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java

Example 5: - To Count items in list box
// countItems()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{ List l;
public void init()
{ l = new List(3,false);
l.addItem("aaa");
l.addItem("bbb");
l.addItem("ccc");
l.addItem("ddd");
l.addItem("eee");
add(l);
}
public boolean mouseUp(Event e, int x, int y)
{ showStatus("Number of items.."+l.countItems());
l.clear();
return true;
}
}
/*
< applet code="zzz.class" width=200 height=200 >
< /applet >
*/
Output: -
C:\java\applet>javac zzz.java
C:\java\applet>appletviewer zzz.java


Free Web Hosting