0% found this document useful (0 votes)
25 views28 pages

AWT Controls

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views28 pages

AWT Controls

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

AWT Controls

Adding and Removing Controls


• In order to include a control in a window, we must add it
to the window.
• So, we must first create an instance of the desired control
and then add it to a window by calling add( ), which is
defined by Container.
• add(Component compObj)
• Here, compObj is an instance of the control that we want
to add.
• A reference to compObj is returned. Once a control has
been added, it will automatically be visible whenever its
parent window is displayed.
• Sometimes we want to remove a control from a window
when the control is no longer needed. For doing this, call
remove( ).
• This method is also defined by Container. It has this
general form:
• remove(Component obj)
• We can remove all controls by calling removeAll( )
Responding to Controls
• Except for labels, which are passive controls, all controls generate
events when they are accessed by the user.
• For example, when the user clicks on a push button, an event is sent
that identifies the push button.
• In general, our program simply implements the appropriate interface
and then registers an event listener for each control that we need to
monitor.
• the AWT supports the following types of controls:
– Labels
– Buttons
– Check boxes
– choice
– Lists
– Scroll Bar
– Textfield
– Text area etc.
1. Labels:
• A label is an object of type Label, and it contains a string ,
which it displays.
• Label defines the following constructors
– Label()
– Label(String str)
– Label(String str, int how)
• The first version creates a blank label.
• The second version creates a label that contains the string
specified by str. This string is left-justified.
• The third version creates a label that contains the string
specified by str using the alignment specified by how.
• The value of how must be one of these 3 constants:
• Label.LEFT , Label.RIGHT , Label.CENTER.
Design a java applet that displays 3 labels Name, address and
phone number with its background color set as green and the text
color should be red.
import java.awt.*;
apps.html (html file)
import java.applet.*;
public class apps extends Applet <html>
{ <applet code=“apps.class”
public void init( ) width=“100”
{ height=“100’></applet>
</html>
Label l1 = new Label("Name");
Label l2 = new Label("Address");
Label l3 = new Label("Phone no:");
setBackground(Color.green); OUTPUT
setForeground(Color.red);
add(l1);
add(l2); Name
add(l3); Address
Phone no:

}
}
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>

• Implementing an interface means supplying methods with the


right signatures.
• To implement the ActionListener interface, the listener class
must have a method called actionPerformed that receives an
ActionEvent object as a parameter.
• Whenever the user clicks the button, the Button object creates
an ActionEvent object and calls
listener.actionPerformed(event), passing that event object.
output
3. Check boxes:
• A check box is a control that is used to turn an option on
or off. It consists of a small box that can either contain a
check mark or not.
• There is a label associated with each check box that
describes what option the box represents.
• You can change the state of a check box by clicking on
it.
• Check boxes are objects of the Checkbox class.
apps.html (html file)
import java.awt.*;
import java.applet.*; <html>
public class apps extends Applet <applet code=“apps.class”
width=“100”
{ height=“100’></applet>
public void init( ) </html>
{
Label l1 = new Label("Name");
Label l2 = new Label("Address");
Label l3 = new Label("Phone no:");
Checkbox c1 = new Checkbox("Agree terms and conditions");

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>

public void init()


{
List os = new List(4, true);

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();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy