AWT Controls
Control Fundamentals
The AWT
supports the following types of controls:
·
Labels
·
Push
buttons
·
Check
boxes
·
Choice
lists
·
Lists
·
Scroll
bars
·
Text
editing
Adding and Removing Controls
·
To
include a control in a window, you must add it to the window
o
first
create an instance of the desired control
o
add
it to a window by calling add( )
o
Component
add(Component compObj)
·
when
the control is no longer needed, we can remove it by calling remove( )
·
void
remove(Component obj)
Labels
·
The
easiest control to use is a label.
·
it
contains a string, which it displays
·
Label
defines the following constructors:
Label( ) : creates a blank label.
Label(String str) : creates a label that contains the
string specified by str
Label(String str, int how) : creates a label that contains the string specified by str
using the alignment
specified by how
·
setText( ) : used
to set or change the text in a label
·
getText( ) : can obtain the current label
Demonstrating Labels
Using Buttons
·
A
push button is a component that contains a label and that generates an event
when it is pressed
·
Button
defines these two constructors:
Button( ) :
creates an empty button
Button(String str) :
creates a button that contains str as a label
·
setLabel(
) : sets the label of the button
·
getLabel(
) : retrive the label of the button
Handling Buttons
·
Each
time a button is pressed, an action event is generated
·
This
is sent to any listeners
·
Each
listener implements the ActionListener interface
·
That
interface defines the actionPerformed( ) method, which is called when an event
occurs.
·
getActionCommand(
):label on the button is retrived
Demonstrating Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet
code="ButtonDemo" width=250 height=150>
</applet> */
public class ButtonDemo extends
Applet implements ActionListener {
String msg = "";
Button yes, no, maybe;
public void init() {
yes = new Button("Yes");
no = new Button("No");
maybe = new
Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void
actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if(str.equals("Yes")) {
msg = "You pressed Yes.";
}
else if(str.equals("No")) {
msg = "You pressed No.";
}
else {
msg = "You pressed
Undecided.";
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 6, 100);
}
}
output
Applying 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
·
Checkbox
supports these constructors
Checkbox( ) : label is initially blank, The state of the
check box is unchecked
Checkbox(String str): label is specified by str, The state of
the check box is unchecked
Checkbox(String str, boolean on):allows to set the initial
state of the check box
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
group is
specified by cbGroup
·
common
functions
getState( ): retrieve the
current state of a check box
setState( ), getLabel( ), setLabel( )
Handling Check Boxes
·
Each
time a check box is selected or deselected, an item event is generated
·
This
is sent to any listeners
·
Each
listener implements the ItemListener interface.
·
That
interface defines the itemStateChanged( ) method
Demonstrating
check boxes
Output
Choice Controls
· Choice control is a form of menu.
· When the user clicks on it, the whole list of choices pops up
· To add a selection to the list, call add( )
· getSelectedItem( ): returns a string containing the name of the item
· getSelectedIndex( ): returns the index of the item
· getItemCount( ): number of items in the list
· select( ) : can set the currently selected item
Handling Choice Lists
· Each time a choice is selected, an item event is generated
· This is sent to any listeners
· Each listener implements the ItemListener interface.
· That interface defines the itemStateChanged( ) method
Demonstrating Choice lists
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*<applet
code="ChoiceDemo" width=300 height=180>
</applet
> */
public class
ChoiceDemo extends Applet implements ItemListener {
Choice os, browser;
String msg = "";
public void
init() {
os = new
Choice();
browser =
new Choice();
// add items to os list
os.add("Windows XP");
os.add("Windows
Vista");
os.add("Solaris");
os.add("Mac
OS");
// add
choice lists to window
add(os);
add(browser);
//
register to receive item events os.addItemListener(this); browser.addItemListener(this);
}
public void
itemStateChanged(ItemEvent ie) {
repaint();
}
//
Display current selections.
public void
paint(Graphics g) {
msg = "Current OS: ";
msg += os.getSelectedItem(); g.drawString(msg,
6, 120);
msg =
"Current Browser: ";
msg +=
browser.getSelectedItem(); g.drawString(msg, 6, 140);
}
}
Using Lists
· The List class provides a compact, multiple-choice, scrolling selection list
· a List object can be constructed to show any number of choices in the visible window
· List provides these constructors:
List( ) throws HeadlessException :allows only one item to be
selected
List(int numRows)
throws HeadlessException: number of entries in the list
List(int numRows, boolean multipleSelect) throws
HeadlessException
Handling Lists
· To add a selection to the list, call add( ).
void add(String name) : Here, name is the name of the item
added to the list.
void add(String name, int index) : adds the item at the index specified by index
item is selected by calling
getSelectedItem( )
- getSelectedItems( )
getSelectedIndex( )
- getSelectedIndexes( )
·
getItemCount(
)
·
select(int
index)
DEMO PROGRAM
//
Demonstrate Lists.
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
/*
<applet
code="ListDemo" width=300 height=180> </applet >
*/
public class
ListDemo extends Applet implements ActionListener {
List os,
browser;
String msg =
"";
public void
init() {
os = new List(4, true);
browser =
new List(4, false);
// add items
to os list
os.add("Windows
XP");
os.add("Windows
Vista");
os.add("Solaris");
os.add("Mac
OS");
// add
items to browser list
browser.add("Internet
Explorer");
browser.add("Firefox");
browser.add("Opera");
browser.select(1);
// add lists
to window
add(os);
add(browser);
//
register to receive action events
os.addActionListener(this);
browser.addActionListener(this);
}
public void
actionPerformed(ActionEvent ae)
{
repaint();
}
//
Display current selections.
public void
paint(Graphics g)
{
int idx[];
msg =
"Current OS: ";
idx = os.getSelectedIndexes();
for(int i=0;
i<idx.length; i++)
msg += os.getItem(idx[i]) + "
";
g.drawString(msg,
6, 120);
msg = "Current Browser: ";
msg +=
browser.getSelectedItem();
g.drawString(msg, 6, 140);
}
}









No comments:
Post a Comment