Chapter 1)
Chapter 1)
Windowing Toolkit
(AWT)
Introduction
• Java AWT (Abstract Windowing Toolkit) is an API to develop
GUI or window-based application in java.
• Java AWT components are platform-dependent i.e. components
are displayed according to the view of operating system.
• AWT is heavyweight i.e. its components uses the resources of
system.
• The java.awt package provides classes for AWT API such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
GUI
Window Fundamentals :
Component
• The Component is abstract class that encapsulates all
the attributes of visual component.
• All User interface (UI) elements that are displayed on
screen are subclasses of Component.
Method Description
• Void addActionListener(ActionListener l)
It adds the specified action listener to get the action
events from the button.
• void removeActionListener(ActionListener l)
It removes the specified action listener so that it
no longer receives action events from the button.
• String getActionCommand()
It returns the command name of the action event
fired by the button.
• The first version creates a List control that allows only one item to be
selected at any one time.
• In the second form, the value of numRows specifies the number of
entries in the list that will always be visible (others can be scrolled into
view as needed).
• In the third form, if multipleSelect is true, then the user may select two
or more items at a time.
Methods
• void add(String name)
It adds the specified item into the end of scrolling list
• void add(String name, int index)
It adds the specified item into list at the given index
position
• String getSelectedItem( )
It gets the selected item on the list
• int getSelectedIndex( )
It fetches the index of selected item of list
• String[ ] getSelectedItems( )
It gets the selected items on the list
• int[ ] getSelectedIndexes( )
It gets the selected indices of the list
• int getItemCount( )
It gets the count/number of items in the list
• void select(int index)
It selects the item at given index in the list
• String getItem(int index)
It fetches the item related to given index position
import java.awt.*;
public class ListExample1
public static void main(String args[])
{
ListExample1() { {
Frame f = new Frame(); new ListExample1();
List l1 = new List(5); }
l1.setBounds(100, 100, 75, 75); }
l1.add("Item 1");
l1.add("Item 2");
l1.add("Item 3");
l1.add("Item 4");
l1.add("Item 5");
f.add(l1);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
ScrollBars
• Scroll bars are used to select continuous values
between a specified minimum and maximum.
• Scroll bars may be oriented horizontally or
vertically.
• It can be added to top-level container like Frame or a
component like Panel.
Constructors
• Scrollbar( )
• Scrollbar(int style)
• Scrollbar(int style, int iValue, int tSize, int min, int
max)
• The first form creates a vertical scroll bar.
• The second and third forms allow us to specify style
Scrollbar.VERTICAL, Scrollbar.HORIZONTAL.
• In the third form, the initial value of the scroll bar is passed
in iValue. The number of units represented by the height of
the thumb is passed in tSize. The minimum and maximum
values for the scroll bar are specified by min and max.
Methods
• void setValues(int iValue, int tSize, int min, int max)
It sets the values of four properties for scroll bar:
value, visible amount, minimum and maximum.
• int getValue( )
It fetches the current value of scroll bar
• void setValue(int newValue)
It sets the value of scroll bar with the given
argument value
• int getMinimum( )
It gets the minimum value of the scroll bar
• int getMaximum( )
It gets the maximum value of the scroll bar
• void setUnitIncrement(int newIncr)
It sets the unit increment for the scroll bar
• void setBlockIncrement(int newIncr)
It sets the block increment from scroll bar
import java.awt.*;
public class ScrollbarExample1 {
ScrollbarExample1() {
Frame f = new Frame("Scrollbar Example");
Scrollbar s = new Scrollbar();
s.setBounds (100, 100, 50, 100);
f.add(s);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]) {
new ScrollbarExample1();
}
}
TextField
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
• Sometimes 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.
• Following are the constructors for TextArea:
• TextArea( )
It constructs a new and empty text area with no
text in it.
• TextArea(int numLines, int numChars)
It constructs a new text area with specified number
of rows and columns and empty string as text.
• TextArea(String str)
It constructs a new text area and displays the
specified text in it.
• TextArea(String str, int numLines, int
numChars)
It constructs a new text area with the
specified text in the text area and specified
number of rows and columns
• TextArea(String str, int numLines, int
numChars, int sBars)
It construcst a new text area with specified
text in text area and specified number of rows
and columns and visibility