Awt Notes1
Awt Notes1
Awt Notes1
Java AWT controls are the controls that are used to design graphical user interfaces or web
applications. To make an effective GUI, Java provides java.awt package that supports various
AWT controls like Label, Button, CheckBox, CheckBox Group, List, Text Field, Text Area,
Choice, Canvas, Image, Scrollbar, Dialog, File Dialog, etc that creates or draw various components
on web and manage the GUI based application.
Container
The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such as
Frame, Dialog and Panel.
Window
The window is the container that have no borders and menu bars. You must use frame, dialog or
another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
Useful Methods of Component class
Method Description
public void setSize(int width,int sets the size (width and height) of the
height) component.
• CheckboxGroup
– getSelectedCheckbox
• Returns the radio button (Checkbox) that is currently selected or null if none is selected
• Checkbox
– In addition to the general methods described in Checkboxes, Checkbox has the following two
methods specific to CheckboxGroup’s:
– getCheckboxGroup/setCheckboxGroup
• Determines or registers the group associated with the radio button
Note: Event-handling is the same as with Checkboxes:
import java.applet.Applet;
import java.awt.*;
public class CheckboxGroups extends Applet
{
public void init()
{ setLayout(new GridLayout(4, 2));
setBackground(Color.LIGHT_GRAY);
setFont(new Font("Serif", Font.BOLD, 16));
add(new Label("Flavor", Label.CENTER));
add(new Label("Toppings", Label.CENTER));
CheckboxGroup flavorGroup = new CheckboxGroup();
add(new Checkbox("Vanilla", flavorGroup, true));
add(new Checkbox("Colored Sprinkles"));
add(new Checkbox("Chocolate", flavorGroup, false));
add(new Checkbox("Cashews"));
add(new Checkbox("Strawberry", flavorGroup, false));
add(new Checkbox("Kiwi")); } }
Java AWT List
The object of List class represents a list of text items. By the help of list, user can choose either
one item or multiple items. It inherits Component class.
• Constructors
● List(int rows, boolean multiSelectable)
• Creates a listbox with the specified number of visible rows (not items)
• Depending on the number of item in the list (addItem or add), a scrollbar is automatically
created
• The second argument determines if the List is multi selectable
• The preferred width is set to a platform-dependent value, and is
typically not directly related to the width of the widest entry
● List()
• Creates a single-selectable list box with a platform-dependent number of rows and a platform-
dependent width
● List(int rows)
• Creates a single-selectable list box with the specified number of rows and a platform-dependent
width
Other List Methods
• add
– Add an item at the end or specified position in the list box
– All items at that index or later get moved down
• isMultipleMode
– Determines if the list is multiple selectable (true) or single
selectable (false)
• remove/removeAll
– Remove an item or all items from the list
• getSelectedIndex
– For a single-selectable list, this returns the index of the selected item
– Returns –1 if nothing is selected or if the list permits multiple selections
• getSelectedIndexes
– Returns an array of the indexes of all selected items
• Works for single- or multi-selectable lists
• If no items are selected, a zero-length (but non-null) array is returned
• getSelectedItem
– For a single-selectable list, this returns the label of the selected item
– Returns null if nothing is selected or if the list permits multiple selections
• getSelectedItems
– Returns an array of all selected items
– Works for single- or multi-selectable lists
• If no items are selected, a zero-length (but non-null) array is returned
• select
– Programmatically selects the item in the list
– If the list does not permit multiple selections, then the previously selected item, if any, is also
deselected
Handling List Events
• addItemListener/removeItemListener
– ItemEvents are generated whenever an item is selected or deselected (single-click)
– Handle ItemEvents in itemStateChanged
• addActionListener/removeActionListener
– ActionEvents are generated whenever an item is doubleclicked or RETURN (ENTER) is
pressed while selected
– Handle ActionEvents in actionPerformed
Labels
Label is an object of type Label. It contains a string, which it displays. Labels are passive
controls that do not support any interaction with the user.
Constructors
● Label() //creates 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
And uses the alignment
how must be one of three constants:Label.LEFT,Label.RIGHT,Label.CENTER
Methods
setText() //set or change a text
getText()//obtain the current label
Scroll Bars
Scroll bar are used to select continues values between a specified minimum and maximum.
Scroll bars may be oriented horizontally or vertically. A scroll bar is actually a composite of
several individual parts. Scrollbar control represents a scroll bar component in order to enable
user to select from range of values.
Constructors:
Scrollbar() //Constructs a new vertical scroll bar.
Scrollbar(int orientation) //Constructs a new scroll bar with the specified orientation
Scrollbar(int orientation, int value, int visible, int minimum, int maximum) //Constructs a new
scroll bar with the specified orientation, initial value, visible amount, and minimum and
maximum values.
Methods :
public addAdjustmentListener(AdjustmentListener al) // Adds an AdjustmentListener.
public int getValue() // Gets the Scrollbar's current position value.
public int setValue(int value)// Sets the Scrollbar's current position value
TextField
The textField component allows the user to edit single line of text.When the user types a key
in the text field the event is sent to the TextField. The key event may be key pressed, Key released
or key typed. The key event is passed to the registered KeyListener. It is also possible to for an
ActionEvent if the ActionEvent is enabled on the textfield then ActionEvent may be fired by
pressing the return key.
Constructors:
TextField() //Constructs a new text field.
TextField(int columns) //Constructs a new empty text field with the specified number of
columns.
TextField(String text)// Constructs a new text field initialized with the specified text.
Methods
String getText() // obtain the string currently contained in the textfield
void setText(String str) //to set the text
import java.awt.*;
class TextFieldExample{
public static void main(String args[]){
Frame f= new Frame("TextField Example");
TextField t1,t2;
t1=new TextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new TextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); } }
TextArea
The TextArea control in AWT provide us multiline editor area. The user can type here
as much as he wants. When the text in the text area become larger than the viewable area the
scroll bar is automatically appears which help us to scroll the text up & down and right & left
Constructors:
● TextArea()
Constructs a new text area with the empty string as text.
● TextArea(int rows, int columns)
Constructs a new text area with the specified number of rows and columns and the empty string
as text.
● TextArea(String text)
Constructs a new text area with the specified text.
● TextArea(String text, int rows, int columns)
Constructs a new text area with the specified text, and with the specified number of rows and
columns.
● TextArea(String text, int rows, int columns, int scrollbars)
Constructs a new text area with the specified text, and with the rows, columns, and scroll bar
visibility as specified.