AWT Control Fundaments || Using AWT controls || Bcis Notes

AWT Control Fundaments || Using AWT controls || Bcis Notes

AWT Control Fundaments

The AWT Control Fundaments support the following types of controls:

• Labels
• Pushbuttons
• Checkboxes
• Choice lists
• Lists
• Scroll bars
• Text Area
• Text Field

The awt control fundaments types are described below:-

• Labels

A label is a GUI control that can be used to display static text. The label can be created using the Label class and its constructors which are listed below:
Label()
Label(String str)
Label(String str, int how)

Some of the methods available in the Label class are as follows:

void setText(String str) – To set or assign text to the label.
String getText() – To retrieve the text of a label.
void set alignment(int how) – To set the alignment of text in a label.
int get alignment() – To get the alignment of text in a label.

• Pushbuttons

A push-button is frequently found in GUI control. A push-button or a button can be created by using the Button class and its constructors which are given below:
Button()
Button(String str)

Some of the methods available in the Button class are as follows:

void setLabel(String str) – To set or assign the text to be displayed on the button.
String getLabel() – To retrieve the text on the button.

When a button is clicked, it generates an ActionEvent that can be handled using the ActionListener interface and the event-handling method is actionPerformed(). If there are multiple buttons we can get the label of the button which was clicked by using the method getActionCommand().

• Checkboxes

A checkbox control can be created using the Checkbox class and its following constructors:
Checkbox()
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)

Following are various methods available in the Checkbox class:

boolean getState() – To retrieve the state of a checkbox.
void setState(boolean on)– To set the state of a checkbox.
String getLabel() – To retrieve the text of a checkbox.
void setLabel(String str) – To set the text of a checkbox.

A checkbox when selected or deselected generates an ItemEvent that can be handled using the ItemListener interface and the corresponding event-handling method is itemStateChanged().

• Drop Down Boxes

A drop-down box or a combo box contains a list of items (strings). When a user clicks on a drop-down box, it pops up a list of items from which user can select a single item. A drop-down box can be created using the Choice class. There is only one constructor in the chosen class using which we can create an empty list.

Following are various methods available in Choice class:

void add(String name) – To add an item to the drop-down list.
String get selected item() – To retrieve the item selected by the user.
int get selectedIndex() – To retrieve the index of the item selected by the user.
int get item count() – To retrieve the number of items in the drop-down list.
void select(int index) – To select an item based on the given index.
void select(String name) – To select an item based on the given item name.
void getItem(int index) – To retrieve an item at the given index.

Whenever a user selects an item from the drop-down box, an ItemEvent is generated. It can be handled using the ItemListener interface and the event-handling method is itemStateChanged().

• Lists

A-List box contains a list of items among which the user can select one or more items. More than one items in the list box are visible to the user. A list box can be created using the List class along with the following constructors:

List()
List(int numRows)
List(int numRows, boolean multiple select)

Following are some of the methods available in the List class:

void add(String name) – To add an item to the list box.
void add(String name, int index) – To add an item at the specified index in the list box.
String get selected item() – To get the item name which is selected by the user.
int get selectedIndex() – To get the item index which is selected by the user.
String[] get selected items() – To retrieve the selected item names by the user.
int[] gets electedIndexes() – To retrieve the selected item indexes by the user.
int get item count() – To retrieve the number of items in the list box.
void select(int index) – To select an item based on the given index.
String getItem(int index) – To retrieve the item at the given index.

• Text Area

A text area is a multi-line text entry control in which the user can enter multiple lines of text. A text area can be created using the TextArea class along with the following constructors:
TextArea()
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)

Following are some of the methods available in the TextArea class:

String getText() – To retrieve the text in the text area.
void setText(String str) – To assign or set the text in a text area.
String get selected text() – To retrieve the selected text in a text area.
void select(int start Index, int end Index) – To select the text in the text field from start index to end index – 1.
boolean is editable() – To check whether the text field is editable or not.
void setEditable(boolean canEdit) – To make a text field editable or non-editable.
void append(String str) – To append the given string to the text in the text area.
void insert(String str, int index) – To insert the given string at the specified index.
void replaceRange(String str, int startIndex, int endIndex) – To replace the text from startIndex to endIndex – 1 with the given string.

• Text Field

A text field or text box is a single-line text entry control that allows the user to enter a single line of text. a text field can be created using the TextField class along with its following constructors:
TextField()
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)

Following are various methods available in TextField class:

String getText() – Retrieves the text in the text field.
void setText(String str) – Assigns or sets the text in the text field.
String get selected text() – Retrieves the selected text in the text field.
void select(int start Index, int end index) – To select the text in the text field from start index to end index – 1.
boolean is editable() – To check whether the text field is editable or not.
void setEditable(boolean canEdit) – To make a text field editable or non-editable.
void setEchoChar(char ch) – To set the echo character of a text field. This is generally used for password fields.
boolean echoCharIsSet() – To check whether the echo character for the text field is set or not.
char getEchoChar() – To retrieve the current echo character.

These are the awt control fundaments that are used in java.

You may also like Frame Windows in JAVA

Be the first to comment

Leave a Reply

Your email address will not be published.


*