ppt la10 java programming 05

60
7/25/2019 Ppt La10 Java Programming 05 http://slidepdf.com/reader/full/ppt-la10-java-programming-05 1/60 Java Programming (J2EE LC) Day 5

Upload: ravi-kumar

Post on 25-Feb-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 1/60

Java Programming(J2EE LC)

Day 5

Page 2: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 2/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd2

Session Plan

Day 5

 – Designing GUI

 – Event Handling

 – Applets

Page 3: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 3/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd3

The java.awt Package

Java helps the programmer to create GUI with the classes in java.awt

AWT stands for Abstract Window Toolkit

Page 4: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 4/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd4

Class Hierarchy

Component

Button

Label

WindowScrollbar

CanvasCheckbox

Choice

Container

TextComponent

 Applet

Panel

Dialog

FrameTextField

TextArea

ScrollPane

Page 5: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 5/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd5

Container Classes

Container classes are classes that can have other components on it

So for creating a GUI, we need at least one Container object 

Three types of containers

 –  Panel : It is a pure container and is not a window in itself. The sole purpose

of a Panel is to organize the components on to a window.

 –  Frame : It is a fully functioning window with its own title and icons.

 –  Dialog : It can be thought of as a pop-up window that pops out when message

has to be displayed. It is not a fully functioning window like the Frame.

Page 6: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 6/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd6

A Simple GUI Program

Just creating a Frame container without any components added to it

importimportimportimport java.awtjava.awtjava.awtjava.awt.*;.*;.*;.*;

classclassclassclass SimpleGUISimpleGUISimpleGUISimpleGUI{{{{

public static voidpublic static voidpublic static voidpublic static void main(Stringmain(Stringmain(Stringmain(String argsargsargsargs[]) {[]) {[]) {[]) {

Frame f = newFrame f = newFrame f = newFrame f = new Frame(Frame(Frame(Frame(““““SimpleSimpleSimpleSimple GUIGUIGUIGUI””””););););

f.setSize(300, 400);f.setSize(300, 400);f.setSize(300, 400);f.setSize(300, 400);

f.setVisible(truef.setVisible(truef.setVisible(truef.setVisible(true););););

}}}}

}}}} Making the Frame visible, withoutthis statement, nothing will be

seen and should be the laststatement after all components

have been added

Setting the size of the Frame

Calling the constructor of theFrame class and passing the titleof the Frame as a String argument

Page 7: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 7/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd7

importimportimportimport java.awtjava.awtjava.awtjava.awt.*;.*;.*;.*;

classclassclassclass SimpleGUISimpleGUISimpleGUISimpleGUI{{{{public static voidpublic static voidpublic static voidpublic static void main(Stringmain(Stringmain(Stringmain(String argsargsargsargs[]){[]){[]){[]){

FrameFrameFrameFrame myFramemyFramemyFramemyFrame= new= new= new= new Frame(Frame(Frame(Frame(““““SimpleSimpleSimpleSimple GUIGUIGUIGUI””””););););

myFrame.setSize(300, 400);myFrame.setSize(300, 400);myFrame.setSize(300, 400);myFrame.setSize(300, 400);

myFrame.setBackground(Color.pinkmyFrame.setBackground(Color.pinkmyFrame.setBackground(Color.pinkmyFrame.setBackground(Color.pink););););

ButtonButtonButtonButton myButtonmyButtonmyButtonmyButton = new= new= new= new Button(Button(Button(Button(““““OkOkOkOk””””););););

myFrame.add(myButtonmyFrame.add(myButtonmyFrame.add(myButtonmyFrame.add(myButton););););

myFrame.setVisible(truemyFrame.setVisible(truemyFrame.setVisible(truemyFrame.setVisible(true););););

}}}}

}}}}

Adding Components

We can add Components to a Container

Creating a Button object andadding onto the container

Page 8: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 8/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd8

Layout Managers

Each Container has a Layout Manager that will decide the size and position 

of the Components placed on it ie each container has its default layout

The programmer can change the default Layout of the Container using the

method setLayout (which layout to follow) 

The programmer can also cancel the default Layout of a Container by calling

the method setLayout (null) 

Page 9: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 9/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd9

Different Layouts (1 of 2)

There are 5 different layouts available

FlowLayout

 – The components are placed horizontally one after another and then move to the

next line

GridLayout

 – The components are placed in a grid (rows, columns)

BorderLayout

 – 5 components can be added at the most

 – The 5 borders are North, South, East, west and Center

 – If not specified the default position is center in Border Layout

Page 10: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 10/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd10

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 11: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 11/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd11

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 12: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 12/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd12

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 13: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 13/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd13

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 14: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 14/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd14

Different Layouts (2 of 2)

CardLayout

 – The CardLayout places components/containers on top of each other like a deck of

cards

 – Only one is visible at a time

 – Every card is made visible using the method show() 

GridBagLayout

 – Most powerful and flexible

 – It is a more advanced form of GridLayout where components can be placed

horizontally and vertically

 – Components can be of different sizes and they can span multiple cells in the grid

Page 15: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 15/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd15

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 16: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 16/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd16

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 17: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 17/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd17

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 18: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 18/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd18

Absolute Positioning

The programmer may take full control of positioning the components on the

container by doing absolute positioning 

 – setLayout(null);

 – setBounds(x-cord, y-cord, width, height);

Page 19: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 19/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd19

Checkbox

Checkboxes are used when you want to select 0 or more items from a set of items

The method getState() will return true if the Checkbox is selected

…………

Checkbox c1 = newCheckbox c1 = newCheckbox c1 = newCheckbox c1 = new Checkbox(Checkbox(Checkbox(Checkbox(““““EnglishEnglishEnglishEnglish””””););););

Checkbox c2 = newCheckbox c2 = newCheckbox c2 = newCheckbox c2 = new Checkbox(Checkbox(Checkbox(Checkbox(““““GermanGermanGermanGerman””””););););Checkbox c3 = newCheckbox c3 = newCheckbox c3 = newCheckbox c3 = new Checkbox(Checkbox(Checkbox(Checkbox(““““FrenchFrenchFrenchFrench””””););););

…………

if (c1.getState())if (c1.getState())if (c1.getState())if (c1.getState())

System.out.println(System.out.println(System.out.println(System.out.println(““““TheTheTheThe user knows Englishuser knows Englishuser knows Englishuser knows English””””););););

Page 20: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 20/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd20

CheckboxGroup

CheckboxGroup can be used to group some Checkbox objects together, so

that they become Radio Buttons 

Radio Buttons are used when we want to select one and only one from a set ofitems

…………

CheckboxGroupCheckboxGroupCheckboxGroupCheckboxGroup cg = newcg = newcg = newcg = new CheckboxGroupCheckboxGroupCheckboxGroupCheckboxGroup();();();();

Checkbox c1 = newCheckbox c1 = newCheckbox c1 = newCheckbox c1 = new Checkbox(Checkbox(Checkbox(Checkbox(““““MorningMorningMorningMorning””””, cg, true);, cg, true);, cg, true);, cg, true);

Checkbox c2 = newCheckbox c2 = newCheckbox c2 = newCheckbox c2 = new Checkbox(Checkbox(Checkbox(Checkbox(““““EveningEveningEveningEvening””””, cg, false);, cg, false);, cg, false);, cg, false);

Checkbox c3 = newCheckbox c3 = newCheckbox c3 = newCheckbox c3 = new Checkbox(Checkbox(Checkbox(Checkbox(““““NightNightNightNight””””, cg, false);, cg, false);, cg, false);, cg, false);

…………

Page 21: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 21/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd21

Choice

Choice is a Drop Down List 

This is used to select 0 or more items from a set of items

The method getSelectedItem() will return the item selected by the user

 –  System.out.println(c.getSelectedItem());

…………Choice c = new Choice();Choice c = new Choice();Choice c = new Choice();Choice c = new Choice();

c.add(c.add(c.add(c.add(““““SundaySundaySundaySunday””””););););

c.add(c.add(c.add(c.add(““““MondayMondayMondayMonday””””););););

…………

Page 22: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 22/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd22

List

List is a scrollable list of items

A list can be used to select 0 or more items from a set of items

The method getSelectedItem will return the item selected by the user

 –  System.out.println(l.getSelectedItem());

 –  getSelectedItems() returns a String[] if multiple items are selected

…………

ListListListList listlistlistlist = new List();= new List();= new List();= new List();

ListListListList listlistlistlist = new List(5,true);= new List(5,true);= new List(5,true);= new List(5,true);

list.add(list.add(list.add(list.add(““““SundaySundaySundaySunday””””););););

list.add(list.add(list.add(list.add(““““MondayMondayMondayMonday””””););););

…………

Page 23: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 23/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd23

Please find more explanation in the notes page

(This slide is intentionally left blank)

Page 24: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 24/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd24

Label

Label is a simple control used to display some message

 –  Label l = new Label(“Hello”) 

The method setText can be used to set the message on the label –  l.setText(“Hello World”); 

The method getText can be used to get the message from the label

 –  System.out.println(l.getText() );

Page 25: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 25/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd25

Scrollbar

Scrollbar is used to increment or decrement some value

The method getValue is used to get the current value of the Scrollbar

 –  System.out.println(s.getValue());

Scrollbar s = newScrollbar s = newScrollbar s = newScrollbar s = newScrollbar(Scrollbar.HORIZONTALScrollbar(Scrollbar.HORIZONTALScrollbar(Scrollbar.HORIZONTALScrollbar(Scrollbar.HORIZONTAL,,,,

25, 10, 0, 100);25, 10, 0, 100);25, 10, 0, 100);25, 10, 0, 100);

Page 26: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 26/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd26

TextField and TextArea

TextField and TextArea permits the user to type in some input

TextField is single line whereas TextArea is multi line 

The method getText gets the text typed in by the user

 –  System.out.println(tf.getText());

…………TextFieldTextFieldTextFieldTextField tftftftf = new TextField(10);= new TextField(10);= new TextField(10);= new TextField(10);

TextAreaTextAreaTextAreaTextArea tatatata = new TextArea(10, 20);= new TextArea(10, 20);= new TextArea(10, 20);= new TextArea(10, 20);

…………

Page 27: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 27/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd27

Please find more explanation in the notes page

(This slide is intentionally left blank)

Page 28: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 28/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd28

MenuBar, Menu and MenuItem (1 of 3)

The classes MenuBar, Menu and MenuItem help us to create a menu system

for the window

MenuBar is the area in the window where the menu system appears

 –  MenuBar mb = new MenuBar(); 

A menu system can be added to a window by setting a MenuBar to this

window –  f.setMenuBar(mb); (where f is the Frame reference)

Page 29: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 29/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd29

MenuBar, Menu and MenuItem (2 of 3)

The different items that appear on the MenuBar are represented by the class

Menu 

A Menu can be added to a MenuBar using the add method

…………

Menu m1 = newMenu m1 = newMenu m1 = newMenu m1 = newMenu(Menu(Menu(Menu(““““FileFileFileFile””””););););

Menu m2 = newMenu m2 = newMenu m2 = newMenu m2 = newMenu(Menu(Menu(Menu(““““EditEditEditEdit””””););););

Menu m3 = newMenu m3 = newMenu m3 = newMenu m3 = new

Menu(Menu(Menu(Menu(““““HelpHelpHelpHelp””””););););

…………

mb.add(mmb.add(mmb.add(mmb.add(m1);1);1);1);

mb.add(mmb.add(mmb.add(mmb.add(m2);2);2);2);

…………

Page 30: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 30/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd30

MenuBar, Menu and MenuItem (3 of 3)

The items that drop down when you select a Menu are objects of MenuItem 

A MenuItem can be added to a Menu using the method add

…………

MenuItemMenuItemMenuItemMenuItem menuitem1 = newmenuitem1 = newmenuitem1 = newmenuitem1 = new

MenuItem(MenuItem(MenuItem(MenuItem(““““OpenOpenOpenOpen””””););););MenuItemMenuItemMenuItemMenuItem menuitem2 = newmenuitem2 = newmenuitem2 = newmenuitem2 = newMenuItem(MenuItem(MenuItem(MenuItem(““““NewNewNewNew””””););););

…………

…………

m1.add(mim1.add(mim1.add(mim1.add(mi1);1);1);1);

m1.add(mim1.add(mim1.add(mim1.add(mi2);2);2);2);

…………

Page 31: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 31/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd31

Panel

Panel is a Container and can contain other Components

Generally we do not use Panel as the main Container since it is not a

Window 

Panel can be used to logically group some Components together

We can add three Panels to a Frame, each Panel can have a different Layout

and Components can be added to the Panels

Page 32: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 32/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd32

Dialog

Dialog is a Window  that is used to display some message and get a

response from the user

For example, a small window that asks the user “Are you sure?” can be

implemented using Dialog

The Dialog can have two Buttons also that says “Yes” and “No”

The method dispose() helps to dispose the dialogbox

Page 33: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 33/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd33

Events

Events correspond to :

 – Physical actions (E.g.: mouse button down, Key press/release)

 – Logical events (E.g.: gotfocus - receiving focus on a component)

Event is an encapsulation of some input delivered asynchronously to the

application

The java.awt.event  package defines classes to represent different type of

events.

Page 34: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 34/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd34

Event Handling Mechanisms

Delegation event model  – The new approach

Concept :

 – Source generates the events and sends them to one or more listeners

 – Listener waits until it receives an event

 – Once received, the listener processes the event and then returns

Event handling is totally separated from UI component

A UI is able to "delegate" the event handling procedure to a separate piece of

code

Page 35: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 35/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd35

Hierarchy of Event Classes in java.awt.event

EventObject

AWTEvent

Action Event AdjustmentEvent ComponentEvent ItemEvent TextEvent

ContainerEvent FocusEvent InputEvent PaintEvent WindowEvent

KeyEvent Mouse Event

Page 36: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 36/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd36

Main Event Classes in java.awt.event (1 of 2)

Abstract superclass for all component input eventclasses.

InputEvent

FocusListenerGenerated when a component gains or loses keyboard

focusFocusEvent

ContainerListenerGenerated when a component is added or removed

from the container.ContainerEvent

ComponentListenerGenerated when a component is moved, resized,

shown or hidden.ComponentEvent

AdjustmentListenerGenerated when a scroll bar is used.AdjustmentEvent

ActionListenerGenerated when a button is pressed.ActionEvent

LISTENERDESCRIPTIONEVENT CLASS

Page 37: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 37/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd37

Main Event Classes in java.awt.event (1 of 2)-(Contd..)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 38: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 38/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd38

Main Event Classes in java.awt.event (1 of 2)-(Contd..)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 39: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 39/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd39

Main Event Classes in java.awt.event(1 of 2)-(Contd..)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 40: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 40/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd40

Main Event Classes in java.awt.event(1 of 2) -(Contd..)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 41: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 41/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd41

Main Event Classes in java.awt.event(1 of 2)-(Contd..)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

Page 42: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 42/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd42

Main Event Classes in java.awt.event (2 of 2)

WindowListenerGenerated when a window is activated, closed,

deactivated, deiconified, iconified, opened.WindowEvent

TextListenerGenerated when the value of a text area or text field is

changed.TextEvent

MouseListenerGenerated when the mouse is moved, dragged,clicked, or released.

MouseEvent

KeyListenerGenerated when input is received from the keyboard.KeyEvent

ItemListenerGenerated when a check box or list item is clickedItemEvent

LISTENERDESCRIPTIONEVENT CLASS

Page 43: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 43/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd43

Main Event Classes in java.awt.event (2 of 2)-(Contd…)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

C f C

Page 44: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 44/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd44

Main Event Classes in java.awt.event (2 of 2)-(Contd…)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

M i E Cl i j (2 f 2) (C d )

Page 45: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 45/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd45

Main Event Classes in java.awt.event (2 of 2)-(Contd…)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

M i E t Cl i j t t (2 f 2) (C td )

Page 46: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 46/60

ER/CORP/CRS/LA10/003Version 1.00

Copyright © 2005, InfosysTechnologies Ltd

46

Main Event Classes in java.awt.event (2 of 2)-(Contd…)

Please find more explanation of the previous slide in the notes page

(This slide is intentionally left blank)

U i th d l ti t d l

Page 47: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 47/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd47

Using the delegation event model

Create a Listener

object which

implements the

listener interface.

Associate a Listener

with the source

public classpublic classpublic classpublic class MyApplicationMyApplicationMyApplicationMyApplication{{{{

............

ButtonButtonButtonButton buttonbuttonbuttonbutton = new= new= new= new Button("I'mButton("I'mButton("I'mButton("I'm a button!");a button!");a button!");a button!");

button.addActionListener(newbutton.addActionListener(newbutton.addActionListener(newbutton.addActionListener(new MyHandlerMyHandlerMyHandlerMyHandler());());());());}}}}

public classpublic classpublic classpublic class MyHandlerMyHandlerMyHandlerMyHandler implementsimplementsimplementsimplements ActionListenerActionListenerActionListenerActionListener{{{{

public voidpublic voidpublic voidpublic void actionPerformed(ActionEventactionPerformed(ActionEventactionPerformed(ActionEventactionPerformed(ActionEvent e){e){e){e){

numClicksnumClicksnumClicksnumClicks++;++;++;++;

}}}}

}}}}

Method in the interface(ActionListener)

Adapter Classes (1 of 2)

Page 48: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 48/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd48

Adapter Classes (1 of 2)

While implementing an interface, we must implement all the methods listed in

the interface

E.g., the MouseListener interface contains five methods: mousePressed,

mouseReleased, mouseEntered, mouseExited, and mouseClicked

If you don’t want to implement some methods, you must have empty bodies of

code for those methods

Difficult to read the code, and maintain it !

Adapter Classes (2 of 2)

Page 49: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 49/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd49

Adapter Classes (2 of 2)

Adapter classes help you avoid implementing empty method bodies

The Java API includes an adapter class for each listener interface with more

than one method.

For example, the MouseAdapter class implements the MouseListener

interface.

An adapter class implements empty versions of all its interface's methods.

Instead of implementing the listener interface, one can extend the

corresponding adapter class and implement only the necessary method(s)

Anonymous inner class

Page 50: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 50/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd50

Anonymous inner class

Sometimes, anonymous inner classes are used for event handling

The general syntax for an anonymous inner class is:

b.addActionListener(((( new ActionListener(){{{{

public void actionPerformed(ActionEventae){{{{

System.out.println(“ButtonClicked”);

}}}}

}}}}))));

Declaring a class without aname which implements the

ActionListener interface.The class declaration and

instantiation is happening inthe same step

new<BaseClassName/InterfaceName>(){

//method implementations

}

Applets

Page 51: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 51/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd51

Applets

A Java class that can be embedded within a HTML page and downloaded

and executed by a web browser

An applet can be used to enhance the features of a Web Page

The applet runs on JVM embedded in the browser 

Applets

Page 52: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 52/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd52

Applets

Applet is a container class that is available in java.applet package

We can create our own Applets by extending the Applet class 

Lifecycle of an Applet (1 of 3)

Page 53: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 53/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd53

Lifecycle of an Applet (1 of 3)

Applet

Working

AppletDisplayed

Idle

State

Applet

Destroyed

start( )

paint( )

stop( )

destroy( )

Draw/Redraw Applet

Destroy

Applet

init( )Start State

Lifecycle of an Applet (2 of 3)

Page 54: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 54/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd54

Lifecycle of an Applet (2 of 3)

The browser calls the init method of the Applet, followed by the start method

If the users leaves the web page, the browser will call the stop method of the

Applet

Lifecycle of an Applet (3 of 3)

Page 55: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 55/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd55

Lifecycle of an Applet (3 of 3)

If the user comes back to the page the browser will call the start method of the

Applet again

The destroy method is called just before the applet is finally unloaded from

memory

Execution of an applet (1 of 2)

Page 56: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 56/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd56

Execution of an applet (1 of 2)

Compile the applet program, say MyApplet.java, to get the .class file,

MyApplet.class

Embed the <applet></applet> tags in a html file as follows

 –  Code, width and height are mandatory attributes for the applet tag

Use any java compatible browser to run the html file

<applet<applet<applet<applet code code code code ====““““MyAppletMyAppletMyAppletMyApplet"""" width width width width =200=200=200=200height height height height =200>=200>=200>=200>

</applet></applet></applet></applet>

Execution of an applet (2 of 2)

Page 57: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 57/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd57

Execution of an applet (2 of 2)

For testing an Applet, we can use the appletviewer tool which is in

<javahome>\bin directory

Type the applet tag alone in a file, say applet.txt, and type the following

command

 – appletviewer applet.txt

Instead of creating a separate file, the applet tag can be included as a

comment in MyApplet.java file itself. The command will now be as follows

 – appletviewer MyApplet.java

The appletviewer tool will open a window to display the Applet

Parameter passing to an applet

Page 58: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 58/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd58

p g pp

Parameters are passed into an applet as name-value pair using the param 

tag within applet tag

 – <applet code ="ParameterApplet" width =200 height =200>

<param name=“name” value=“value">

</applet>

The values are passed as String values

getParameter (String parameterName) returns the value

Summary

Page 59: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 59/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd59

y

Abstract Window Toolkit basics

Abstract Window Toolkit controls

Layout Managers

Event Handling Mechanisms

Event Model

APIs to handle events

Applets

Page 60: Ppt La10 Java Programming 05

7/25/2019 Ppt La10 Java Programming 05

http://slidepdf.com/reader/full/ppt-la10-java-programming-05 60/60

ER/CORP/CRS/LA10/003

Version 1.00Copyright © 2005, Infosys

Technologies Ltd60

Thank You!