pravin yannawar, docs, nmu jalgaon. basic java : event handling in awt and swing 2 objectives of...

19
Pravin Yannawar, DOCS, NMU Jalgaon

Upload: malcolm-norris

Post on 14-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Pravin Yannawar, DOCS, NMU Jalgaon

Page 2: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing2

Objectives of This Session

• Explain the Event handling mechanism & demonstrate it using example

• List the categories of Events & Listener interfaces

• Demonstrate the above example using an independent event handler class

• Identify the need for adapter classes & demonstrate it using an independent event handler example

Page 3: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing3

Event Handling

• GUI applications are event-driven applications.

• They generate events when the user of the program interacts with the GUI.

• The underlying OS is constantly monitoring these events.

• When a event occurs, the OS reports these events to the programs that are running.

• The application will handle the event using a appropriate “Event Handler”.

Page 4: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing4

Delegation Event Model

• Source generates an event & sends it to one or more listeners.

• Listener simply waits until it receives an event.

• Once received, listener processes the event & returns.

Advantage : application logic that processes event is clearly separated from the UI logic that generates those events.

An UI element is able to delegate the processing of an event to a separate piece of code ( Event handler)

Page 5: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing5

Delegation Event Model

• Event: is an object that describes a state change in a source.

• Event Source: is an object that generates an event.

A source must register listeners for listeners to receive notifications about a specific type of event.

• Event Listener : is an object that is notified when an event occurs.

It must be registered with a source. It must implement methods to receive & process

these notifications.

Page 6: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing6

Event Handling

import java.awt.*;import java.awt.event.*;class MyFrame extends Frame implements ActionListener{

TextField t1; Button b1; MyFrame() {

t1 = new TextField(20);b1 = new Button(“Click”);add(t1,”North”); b1.addActionListener(this);add(b1,”South”);

}

Page 7: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing7

Event Handling

public void actionPerformed( ActionEvent e ) {

t1.setText(“Click pressed”);}public static void main(String args[])

{Frame f = new MyFrame();

f.setSize(100,100); f.show();}

}

Page 8: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing8

Inheritance diagram of the AWT Event Classes

EventObject

AWT Event

ComponentEvent

ActionEvent

AdjustmentEvent

ItemEvent

TextEvent

InputEvent

ContainerEvent

FocusEvent

PaintEvent

WindowEvent

MouseEvent

KeyEvent

Page 9: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing9

Event Listener Interfaces

ActionListener

AdjustmentListener

ComponentListener

FocusListener

ItemListener

MouseListener

MouseMotionListener

KeyListener

TextListener

WindowListener

ContainerListener

java.util.EventListener

Page 10: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing10

Event Handling Summary

Interface Methods Parameter EventsGenerated by

ActionListner actionPerformed ActionEventgetActionCommandgetmodifiers

ButtonListMenuItemTextField

AdjustmentListner

adjustmentValueChanged

AdjustmentEventgetAdjustablegetAdjustmentTypegetvalue

Scrollbar

ItemListner itemStateChanged ItemEventgetItemgetItemSelectablegetstateChange

CheckboxCheckboxMenuItemChoiceList

TextListner textValue Changed TextEvent TextComponent

Page 11: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing11

Event Handling Summary

Interface Methods Parameter EventsGenerated by

ComponentListener

componentMovedcomponentHiddencomponentResized

ComponentEventgetComponent

Component

ContainerListener

componentAddedcomponentRemoved

ContainerEventgetChildgetContainer

Container

FocusListener focusGainedfocusLost

focusEventIsTemporary

Component

KeyListener keyPressedkeyRealsedkeyTyped

KeyEventgetKeyChargetKeyCodegetkeyModifiersTextisActionKey

Component

Page 12: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing12

Event Handling Summary

Interface Methods Parameter EventsGenerated by

MouseListener

mousePressedmouseRealesedmouseEnteredmouseExitedmouseClicked

MouseEventgetClickCountgetXgetYgetPointtranslatePointisPopupTrigger

Component

MouseMotionListener

mouseDraggedmouseMoved

Component

WindowListener

windowClosingwindowOpenedwindowIconedwindowDeiconedwindowClosedwindowActivatedwindowDeactivated

WindowEventgetWindow

Window

Page 13: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing13

Delegation Event Model with Another Class

import java.awt.*;class MyFrame extends Frame{

TextField t1; Button b1;

MyFrame() {t1 = new TextField(20);b1 = new Button(“Click”);add(t1,”North”); b1.addActionListener(new

ButtonHandler(this));add(b1,”South”);

}

Page 14: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing14

Delegation Event Model with Another Class

class ButtonHandler implements ActionListener { MyFrame f ; ButtonHandler(MyFrame mf)

{f=mf;

}public void actionPerformed( ActionEvent e ) {

// code for processing button press.}

}

Page 15: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing15

Adapter Classes

• Many of the listener interfaces have more than one method.

• Thus, if a particular interface is implemented, all the methods of that interface should also be implemented.

• To simplify this task, listener interfaces with more than one method come with adapter classes.

• Adapter classes implement all the methods of an interface.

• You can extend the adapter class to specify the desired reaction to some methods.

Page 16: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing16

Without Adapter Classes

class MyFrame extends Frame implements WindowListener

{public void windowActivated(WindowEvent e){}public void windowDeactivated(WindowEvent e){}public void windowIconified(WindowEvent e){}public void windowDeiconified(WindowEvent e){}public void windowOpened(WindowEvent e){}public void windowClosed(WindowEvent e){}public void windowClosing(WindowEvent e){

System.exit(0);}

Page 17: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing17

Without Adapter Class

public static void main(String args[]) {

Frame f = new MyFrame(); f.setSize(100,100); f.show();

f.addWindowListener(f);}

}

Page 18: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing18

Using Adapter Class

import java.awt.*;import java.awt.event.*;

class MyFrame extends Frame{

MyFrame() {

addWindowListener(new WindowHandler()); }

}

Page 19: Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate

Basic Java : Event handling in AWT and Swing19

Adapter Classes

class WindowHandler extends WindowAdapter {{

public void windowClosing(WindowEvent e){

System.exit(0);}

}