Unit 4 - 1 - Awt Components or Controls
Unit 4 - 1 - Awt Components or Controls
o Labels
o Push buttons
o Check boxes
o Choice lists
o Lists
o Scroll bars
o Text editing
Responding to Controls
The HeadlessException
Labels
The easiest control to use is a label. A label is an object
of type Label, and it contains a string, which it
displays.
You can set the alignment of the string within the label
by calling setAlignment( ).
// Demonstrate Labels
import java.awt.*;
import java.applet.*;
/*
<applet code="LabelDemo" width=300 height=200>
</applet>
*/
}
}
// Demonstrate Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo" width=250 height=150>
</applet>
*/
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
if(str.equals("Yes"))
{
msg = "You pressed Yes.";
}
else if(str.equals("No"))
{
msg = "You pressed No.";
}
else
{
msg = "You pressed Undecided.";
}
repaint();
}
The third form allows you to set the initial state of the
check box. If on is true, the check box is initially
checked; otherwise, it is cleared.
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo" width=250
height=200>
</applet>
*/
public class CheckboxDemo extends Applet
implements ItemListener
{
String msg = "";
add(winXP);
add(winVista);
add(solaris);
add(mac);
winXP.addItemListener(this);
winVista.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
{
String msg = "";
Checkbox winXP, winVista, solaris, mac;
CheckboxGroup cbg;
add(winXP);
add(winVista);
add(solaris);
add(mac);
winXP.addItemListener(this);
winVista.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
}
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
void select(int index)
void select(String name)
os.addItemListener(this);
browser.addItemListener(this);
}
Using Lists
String getSelectedItem( )
int getSelectedIndex( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
int getItemCount( )
void select(int index)
Handling Lists
To process list events, you will need to implement the
ActionListener interface. Each time a List item is
double-clicked, an ActionEvent object is generated. Its
getActionCommand( ) method can be used to
retrieve the name of the newly selected item. Also,
each time an item is
selected or deselected with a single click, an
ItemEvent object is generated. Its
getStateChange( ) method can be used to determine
whether a selection or deselection triggered this event.
getItemSelectable( ) returns a reference to the
object that triggered this event.
int getValue( )
void setValue(int newValue)
int getMinimum( )
int getMaximum( )
They return the requested quantity.
Using a TextField
The third form initializes the text field with the string
contained
in str.
The fourth form initializes a text field and sets its width.
String getText( )
void setText(String str)
Here, str is the new string.
String getSelectedText( )
void select(int startIndex, int endIndex)
getSelectedText( ) returns the selected text. The
select( ) method selects the characters beginning at
startIndex and ending at endIndex–1.
boolean isEditable( )
void setEditable(boolean canEdit)
boolean echoCharIsSet( )
char getEchoChar( )
Handling a TextField