Chapter1 AWT
Chapter1 AWT
(22517)
02 Swings 10
03 Event Handling 12
04 Networking Basics 10
06 Servlets 14
Course Outcomes
a) Develop programs using GUI Framework(AWT
and Swing).
b) Handle Events of AWT and Swing components.
c) Develop programs to handle events in java
programming.
d) Develop programs using networking concepts.
e) Develop programs using database.
f) Develop programs using Servlets.
Unit-1
Introduction to
Abstract Windowing
Toolkit (AWT)
4
12 Marks
Unit-1 Outcomes
a) To develop Graphical User Interface (GUI)
programs using AWT components
b) Create Frame window with specified AWT
components
c) Arrange the GUI components using different
layout.
d) Develop programs using Menu and Dialog
boxes
GUI (Graphical User Interface)
• GUI offers user interaction via some graphical
components.
• Graphical components such as Window, frame,
Panel, Button, Textfield, TextArea, Listbox,
Combobox, Label, Checkbox, etc.
• Using these components we can create an
interactive user interface for an application.
• GUI is entirely event based.
• GUI provides result to end user in response to
raised event.
GUI (Graphical User Interface)
button menus title bar menu bar combo box
scroll
bars
AWT (Abstract Window Toolkit)
• AWT contains numerous classes and methods
that allow us to create and manage window.
• import java.awt.*;
• Java AWT 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 components are heavyweight.
1.1 AWT Class Hierarchy
Object
Component
Many more
Container Label Button AWT
conponents
Window Panel
Method Description
public void inserts a component
add(Component c) on this component.
public void setSize(int sets the size (width
width,int height) and height) of the
component.
public void defines the layout
setLayout(LayoutMan manager for the
ager m) component.
public void changes the visibility
setVisible(boolean of the component, by
status) default false.
Component
• MCQ
• Which object is responsible for remembering
the current foreground and background colors
and the currently selected text font?
A) Container
B) Component
C) Window
D) Panel
13
Container
• Container class is a subclass of Component.
l
Applets
• Applet is a public class which is predefined by
java.applet.Applet.
• It is a small program that can be placed on
web.
• There is no main() method. So run using web
browser or appletviewer.
• Life cycle methods: init, start, paint, stop,
destroy
• Applet is container. Default layout is
FlowLayout.
• Applet is subclass of Panel.
• Applet is superclass of JApplet
• MCQ
1) ____ is a superclass of Applet.
A)Frame
B)Panel
C)Dialog
D)None of the above
e
1.2 Creating Windowed program
and Applets
• Extends Frame class
• Constructors are:
Frame()
Frame(String title)
• Setting and Getting window size:
• D:\>java FirstFrame
1.3 AWT Controls & Layout
Manager
• AWT Controls: Component which allows you to
interact with application.
• Labels
• Button
• Checkbox
• Checkbox group
• Scrollbars
• Text field
• Text Area
Some types of components
• Layout Manager: Positioning the components in the
container.
• Flow Layout
• Border Layout
• Grid Layout
• Card Layout
• Grid Bag Layout
• Menubars, menus, dialog boxes, file
dialog.
AWT Controls:
• Allows user to interact with application.
• Adding the control in Window
First create instance of control.
Call add() method defined by component
Component add(Component compObj)
• Removing the Control
Call remove() method defined by component
void remove(Component obj)
For remove all: removeAll() method call.
AWT Control: Label
• Used to just display string on window.
• Passive Component
• Constructors:
Label( )
Label(String str) //left - justified
Label(String str, int how) // Label.LEFT,
Label.RIGHT, Label.CENTER
• Methods to perform operation
About text:
1) void setText(String str)
2) String getText( )
About Alignment
1) void setAlignment(int how)
2) int getAlignment( )
AWT Control: Button
• It contains a label and that generates an
event when it is
pressed.
• Active Components
• Constructors:
• Button( )
• Button(String str)
• Methods to perform operation: Setter and
Getter Method.
• void setLabel(String str)
• String getLabel( )
AWT Control: Button Handling
• When Button pressed which generates an event
when it is pressed.
• Implements ActionListener interface.
• Interface has defined actionPerformed()
method, called when event is generated.
• ActionEvent object is supplied as argument to
the
method.
• ActionEvent object refers both Button and
Label of
Button
• Label will get by using getActionCommand()
from ActionEvent which passed.
AWT Control: CheckBox
• Used to turn an option on or off.
• Small box: check mark or not.
• Each check box has label.
• Constructors are:
• Checkbox( )
• Checkbox(String str)
• Checkbox(String str, boolean on)
• Checkbox(String str, boolean on, CheckboxGroup
cbGroup)
• Checkbox(String str, CheckboxGroup cbGroup,
boolean on)
AWT Control: CheckBox
• To retrieve item:
• String getItem(int index)
• To get Item Count
• int getItemCount( )
• Active Item
• void select(int index)
AWT Control: List
Handling
Button b1=new
Button(“Submit”);
b1.setBounds(50,50,100,50);
add(b1);
Arranging components :
Layout Manager
ipadx, ipady:
Specifies the internal padding: how much
to add to the size of the component. The
default value is zero
MenuBar and
Menu
MenuBar and Menu
Top-level window can have a menu bar associated with
it.
A menu bar displays a list of top-level menu choices.
Each choice is associated with a drop-down menu.
Classes:
MenuBar : Contains one or more Menu objects
Menu : Contains one or more MenuItem objects
MenuItem : Object something selected by user.
It is also possible to include checkable menu items
These are menu options of type CheckboxMenuItem
and will have a check mark next to them when they are
selected.
MenuBar and Menu
To create a menu bar, first create an instance of
MenuBar.
Set MenuBar using setMenuBar(MenuBarObject)
Next, create instances of Menu that will define the
selections displayed on the bar.
Constructors:
Menu( )
Menu(String optionName)
Menu(String optionName, boolean removable)
Individual menu items constructors:
MenuItem( )
MenuItem(String itemName)
MenuItem(String itemName, MenuShortcut keyAccel)
MenuBar and Menu
Disable or enable a menu item by using:
void setEnabled(boolean enabledFlag)
boolean isEnabled( )
Label set and get using:
void setLabel(String newName)
String getLabel( )
Checkable menu item by using a subclass of
MenuItem
called CheckboxMenuItem. :
CheckboxMenuItem( )
CheckboxMenuItem(String itemName)
CheckboxMenuItem(String itemName, boolean on)
Dialog
Box
Dialog boxes are primarily used to obtain user input.
They are similar to frame windows, except that dialog
boxes are always child windows of a top-level window.
Dialog boxes don’t have menu bars.
In other respects, dialog boxes function like frame
windows.
Dialog boxes may be modal or modeless.
When a modal dialog box is active, all input is directed to
it until it is opened. For ex: open dialogbox, save
dialogbox, print dialogbox
When a modeless dialog box is active, input focus can be
directed to another window in your program.
For ex: find & replace dialogbox, spelling and grammer
check dialog box
Dialog
Box
Constructors:.
Dialog(Frame parentWindow, boolean mode)
Dialog(Frame parentWindow, String title, boolean
mode)
To create Dialog Box:
Create Frame or Applet
FileDialog(Frame parent)
Methods:
String getDirectory( ) String getFile( )