The Awt
supports the following types of controls:
o Labels
o Push buttons
o Check boxes
o Choice list
o Lists
o Scroll bar
o Text editing
Labels: A label is an object of type Label, and it contains
a string, which it displays. Labels are passive controls that do not support
any interaction with the user. Label define the following constructors:
Label () throw HeadlessException
Label (String str) throws Headlessexception
Label (string str,int how)throw HeadlessException
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 alignment specified by how. The value of how must be one
three constants: Label.LEFT, Label.RIGHT, or Label.CENTER.
We can set or change the text in a label by using
the setText() method, We can obtain the
current label by calling getText(). The methods are:
void
setText(string str)
String
getText()
Button: The most widely used control is the push button. 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() throw HeadlessException
Button(String str)throws HeadlessException
The first version creates a empty button. The second
version creates a button that contains str as a label.
After a button has been created, you can set its
label, by calling setLabel(). We can retrieve its label by calling getLabel().
The methods are:
void setLabel(String str)
String getLabel()
Here,
str the new label for the button.
Handling
button: Each time a button is
pressed, an action event is generated. This is sent to any listeners that
previously registered an interest in receiving action event notification from
that component. Each listener implements the ActionListener interface. This interface defines the
actionPerformed() methods, which is called when an event occurs. An ActionEvent
object is supplied as the argument to this method. getSource() methods to the button objects that
we added to the window.
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. Check boxes are object objects of
the Checkbox class. Checkbox support this constructor:
Checkbox()throw
HeadlessException
Checkbox(String
str)throw HeadlessException
Checkbox(String
str,boolen on)throw HeadlessException
Checkbox(String str,boolen on,CheckboxGroup cbGroup)
throw HeadlessException
Checkbox(String str,CheckboxGroup cbGroup,boolen on)
throw HeadlessException
The first form creates a check box whose label is
initially blank. The state of this checked box is unchecked. The second form
creates a check box whose label is specified by str. The state of this checked
box is unchecked. 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. The fourth and fifth forms creates a check box whose label specify by
the str and whose group is specified by cbGroup.
To retrieve the current state of a check box, call
getState(). To set its state, call setState().We can obtain the current label
associated with a check box by calling getLabel().To set the label, call
setLabel().These methods are
boolean
getState()
void setState(Boolean on)
String getLabel()
void setLabel(String str)
Handling
Checkbox: Each time a check box
is selected or deselected, an item event is generated. This is sent to any
listeners that previously registered an interest in receiving action event
notification from that component. Each listener implements the ItemListener interface.
Choice
list: The choice class is
used to create a pop-up list of items from which the user may choose. To add a
selection to the list, call add().It has this general form:
void
add(String name)
Here, name is the name of the item being added.
Items are added to the list in the order in which call to add() occur. To
determine which item is currently selected, we may call either getSelectItem()
or getSelectIndex(). These methods are:
String getSelectItem()
int getSelectIndex()
The getSelectItem() methods return a string containing
the name of the item.
The getSelectIndex() methods return the index of the
item. The first item is at index 0.
By default, the first item added to the list is selected.
To obtain the number of items in the list, call
getItemCount(). We can set the currently selected item using the select()
methods with either a zero-based integer index or a string that will match a
name in the list.
int
getItemCount()
void
select(int index)
void select(String name)
We can obtain the name associated with the item at
that index by calling getItem(), which has this general form :
String getItem(int index)
Here,
index specifies the index of the desired element.
Handling
Choice List: Each item a
choices is selected, an item event is generated. This is sent to any listeners
that previously registered an interest in receiving action event notification
from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChange() methods. An ItemEvent
object is supplied as the argument to this method.
Lists: The List class provides a compact multiple choices,
scrolling selection list. The choice object, which shows only the single
selected item in the menu. List provides these constructors:
List() throw headlessException
List(int numRows) throw headlessException
List(int numRows,boolean multipleSelect )
throw headlessException
The first version creates a list control that allows
only one item to be selected at any one
item. In second version, the value of numRows specifies the number of
entries in the list that will always be visible. In the third form, if
multipleSelect is true, then the user may select one or more item at a time.
To add a selection to the list , call add().
void add(String name)
void add(String name,int index)
Here name is the name of the item added to the list. The
first form adds item to the end
of the list. The second form adds the item in the index specified by the index.
String getSelectedItem()
int getSelectedIndex()
The getSelectedItem()
methods return a string containing the name of the item. If more than one item
is selected, or if no selected, or if no selection has been made, null is
returned. getSelectIndex() methods
return the index of the item.
Handling
List: We will need to
implement the ActionListener interface. Each time a List item is double
clicked, an ActionEvent object is generated. Its getActionCommand() methods can
be used to retrieve the name of the
newly selected item.each item an item is selected or deselected with a single
click, an ItemEvent object is generated .getStateChange() can be used to
determine whether a selection or deselection
triggered this event.
Scroll Bars: Scroll Bars are used to select continuous values
between a specified minimum and maximum Scroll bars may be oriented
horizontally or vertically. Scroll bar are encapsulated by the Scrollbar class.
Scrollbar define the following constructor :
Scrollbar()throws HeadlessException
Scrollbar(int style)throws HeadlessException
Scrollbar(int style,int initialValue,int
thumbSize,int min,int max)throws HeadlessException
The first form creates a vertical scroll bar. The
Second and third forms allow we to specify orientation of the scroll bar. If
style is scrollbar.VERTICAL, a vertical scroll bar created. If style is scrollbar.HORIZONTAL,
a horizontal scroll bar created. The third form of the constructor, the initial
value of the scroll bar is passed in initialValue.
The number of units represented by the height of the
thumb is passed in thumbsize. The minimum and maximum values for the scroll bar
are specified by min and max.
void setValues(int initialvalue,int
thumbsize,int min,int max)
The same meaning as they have in the third constructor
just described:
To obtain current value of the scroll bar call
getValue(). It returns the current string. To set the current value, call
setValue(). These methods are:
int getValue()
void setValue(int newValue)
Here, new value means the new value of the scroll
bar. We can also retrieve the minimum and maximum values via getMinimum() and
getMaximum() as shown here:
int getMinimum()
int getMaximum()
Handling
Scroll bar: We need to
implement the AdjuestmentListener interface. Each time a user interface with a
scroll bar, an AdjustmentEvent object is generated. Its getAdjustmentType()
method can be used to determine the type of the adjustment.
TextArea: Some time 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. The
following constructors are used for TextArea :
TextArea()throws HeadlessException
TextArea(int numList,int numChars) throws
HeadlessException
TextArea(String str) throws HeadlessException
TextArea(String str,int numLines,int numChars)
throws HeadlessException
TextArea(String str,int numLines,int
numChars,int sBars) throws HeadlessException
Here, numLines specifies the height, in lines of the
text area, and numChars specifies its Width in character. Initial text can be
specified by str. In the fifth form, you can specifies the scroll bars that you
want the control to have sBars must be one of this values.
TextArea is a subclass of TextComponent. Therefore,
it supports the getText(), setText(), getSelectedText(), select(), isEditable(),
setEditable() methods.
void
append(string str)
void
insert(String str,int index)
void
replaceRange(String str,int Index,int endIndex)
The append() methods append the string specified by
str to the end of the current text.insert() insert the starting passed in str
at the specified index. To replace text, call replaceRange(). It replace the
character from startIndex to the endIndex-1, with the replacement text passed
in str.
TextField: The TextField class implements a single-line
text-entry area, usually called an edit control. Text fields allow the user to
enter the strings and to edit the text using the arrow keys. cut and paste
keys, and mouse selections. TextFields is a subclass of TextComponent.
TextField()throws HeadlessException
TextField(int numChars)throws HeadlessException
TextField(String str) throws HeadlessException
TextField(String str,int numChars) throws
HeadlessException
The first version creates a default text field. The
second version creates a text fields that is numChars characters wide. The
third version initialize the text field call with the string contain in str. The
fourth form initializes a text field and sets its width.
TextField (and its superclass TextComponent)
provides several methods that allow us to utilize a text field. To obtain the
string currently contained in the text field, call getText(). To set the text call the setText().
String getText()
void setText(String str)
Here, str in the
new string.
The user can select a portion of the text in a text
filed. Also we can select a portion of text under program control by using select(). We can obtain the currently
selected text by calling getSelectText().
These methods are:
String
getSelectText()
void select(int
startIndex,int endIndex);
getSelectText() returns the selected text. The select() method select the characters beginning at startIndex and
ending at endIndex -1.
We can modify the text in the text field by calling
the setEditable(). We can determine it
by calling isEditable(). These
methods are:
boolen
isEditable()
void
setEditable(Boolean canEdit)
isEditable() returns true if the text may be changed and false
if not. In setEditable(), if canEdit
is true, the text may be changed. If it is false the text cannot be altered.
void setEchoChar(Char ch)
boolen echoCharIsSet()
char getEchoChar()
Here, ch specifies
the character to be echoed.
Therefore
we can display the echoing of the character as they are typed by calling setEchoChar(). We can check a text
field to see if it is in this mode with the echoCharIsSet() method. We can retrieve the echo character by
calling getEchoChar() method.
No comments:
Write comments