Professional Documents
Culture Documents
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
Container
Window Panel
Frame Canvas
• Methods:
• void setFont(Font f)
• void setLayout(LayoutManager mgr)
• void add(Component object)
• void remove(Component object)
Window
• This class is base class for two window classes-
Panel and Frame.
• We cannot place any component objects into
window class. Generally, we create Panel or
Frame class objects as top-level-window and
place different components into them.
• The window is the container that have no
borders and menu bars.
Panel
• Panel class is concrete subclass of Container so it
implements container. It doesn’t add new methods.
• The Panel is the container that doesn't contain title
bar and menu bars and Borders.
• It can have other components like button, textfield etc.
Canvas
• Canvas is the black window on which we can
draw different graphical objects.
Frame
• It is subclass of Window.
• The Frame is the container that contain title bar and
can have menu bars,borders, and resizing corners.
• It can have other components like button, textfield,
etc.
• Methods:
• void setTitle(String title)
• void setBackground(Color bgcolor)
Working with Frame Window
• Extends Frame class
• Constructor are:
• Frame()
• Frame(String title)
• Setting and Getting window size:
• void setSize(int width, int height)
• void setSize(Dimension newsize)
• Showing and Hiding Frame
• void setVisible(boolean visibleFlag)
● setTitle() Method
void setTitle(String title)
● windowClosing() Method
We cannot close a frame window by clicking on close control of
window by default
Frame Class
• We can create stand-alone AWT based applications.
• A Frame provides main window for the GUI application.
• There are two ways to create a Frame :
1.By instantiating Frame Class
2.By extending Frame class
Program using Frames
import java.awt.*;
class FirstFrame{ public static void main(String args[]){
• 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() { {
new ListExample1();
Frame f = new Frame(); }
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