AWT Controls
AWT Controls
}
}
2. Buttons:
• A button is a component that contains a label and that
generates an event when it is pressed.
• Buttons are objects of type Button.
• Button defines two constructors:
– Button()
– Button(String str)
• The first version creates an empty button.
• The second creates a button that contains str as a label.
Example of a button click
import java.awt.*;
import java.awt.event.*;
public class ActionEventDemo1 extends Applet implements ActionListener
{
public void init()
{
Button b1 = new Button("Submit");
add(b1);
setLayout(new FlowLayout());
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed");
}
}
Html file
<html>
<applet code=“ActionEventDemo1.class” width=
“100” height=“100” > </applet>
</html>
add(l1);
add(l2);
add(l3);
add(c1);
}
}
• Checkbox group:
• It is possible to create a set of mutually exclusive 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.
• To create a set of mutually exclusive check boxes, you must
first define the group to which they will belong and then
specify that group when you construct the check boxes.
• Check box groups are objects of type CheckboxGroup.
Only the default constructor is defined, which creates an
empty group.
• You can determine which check box in a group is currently
selected by calling getSelectedCheckbox( )
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CBGroup extends Applet implements ItemListener
{
String msg = "";
CheckboxGroup cbg;
public void init()
{
cbg = new CheckboxGroup();
Checkbox male = new Checkbox("Male", cbg, true);
Checkbox female= new Checkbox("Female", cbg, false);
add(male);
add(female);
male.addItemListener(this);
female.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{ CBGroup.html (html file)
repaint(); <html>
<applet code=“CBGroup.class”
} width=“100”
public void paint(Graphics g) height=“100’></applet>
</html>
{
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
msg=" ";
}
}
4. Choice control:
• 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.
• Each item in the list is a string that appears as a left
justified label in the order it is added to the Choice
object.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ChoiceDemo extends Applet implements ItemListener
{
Choice co;
String msg = "";
public void init()
{
co = new Choice();
ChoiceDemo.html (html file)
co.add("India");
co.add("America"); <html>
co.add("Africa"); <applet code=“ChoiceDemo”
co.add("Australia"); width=“100”
height=“100’></applet>
add(co); </html>
co.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg += co.getSelectedItem();
g.drawString(msg, 6, 100);
msg=" ";
}
}
5. Lists:
• the list class provides a compact, multiple-choice,
scrolling selection list.
• Unlike the choice object, which shows only the single
selected item in the menu, a List object can be
constructed to show any number of choices in the
visible window. It can also be created to allow
multiple selections.
• Constructors:
• List()
• List(int numRows)
• List(int numRows, boolean multipleselect)
• The first version creates a list control that allows only
one item to be selected at any one time.
• In the second form, the value of numRows specifies
the number of entries in the list that will always be
visible
• If multipleSelect is true, then the user may select two
or more items at a time.
• If it is false, then only one item may be selected.
• To add a selection to the list , call add().
ListDemo.html (html file)
import java.awt.*;
import java.applet.*; <html>
<applet code=“ListDemo.class”
public class ListDemo extends Applet width=“100”
{ height=“100’></applet>
</html>
os.add(“India");
os.add(“America");
os.add(“Africa");
os.add(“Australia");
add(os);
}
}
6. ScrollBar
• The object of Scrollbar class is used to add horizontal and
vertical scrollbar. Scrollbar is a GUI component allows
us to see invisible number of rows and columns.
• It consists of the following constructors
• Scrollbar()
• Scrollbar(int style)
• The first form creates a vertical scroll bar.
• The second form allow us to specify the orientation of the
scroll bar.
• If style is Scrollbar.VERTICAL, a vertical scroll bar is
created.
• If style is Scrollbar.HORIZONTAL, the scroll bar is
horizontal
import java.awt.*;
public class ScrollBar
{
Frame frame;
Label label ;
ScrollBar()
{
frame = new Frame("Scrollbar");
label = new Label("Displaying a horizontal and verticial Scrollbar in the Frame");
Scrollbar scrollB1 = new Scrollbar(Scrollbar.HORIZONTAL);
Scrollbar scrollB2 = new Scrollbar(Scrollbar.VERTICAL);
frame.add(scrollB1,BorderLayout.SOUTH);
frame.add(scrollB2,BorderLayout.EAST);
frame.setSize(370,270);
frame.setVisible(true);
}
}
public static void main(String... ar)
{
ScrollBar sb = new ScrollBar ();
7. TextField
Text field:
• 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.
TextArea:
• Sometimes a single line of text input is not enough
for a given task. To handle these situations, the awt
includes a simple multiline editor called TextArea.
Example 1.
import java.awt.*;
import java.applet.*;
public class apps extends Applet apps.html (html file)
{ <html>
<applet code=“apps” width=“100”
public void init( ) height=“100’></applet>
</html>
{
TextField t1 = new TextField();
TextArea t2 = new TextArea();
add(t1);
add(t2);
}
}
Example 2.
import java.awt.*;
import java.awt.event.*;
public class TextFieldExample extends Frame implements ActionListener
{
TextField tf1,tf2,tf3;
Button b1,b2;
TextFieldExample()
{
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e)
{
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args)
{
TextFieldExample txtfd= new TextFieldExample();
}
}