What is Event

Ashmeeprasad18 776 views 65 slides Aug 25, 2016
Slide 1
Slide 1 of 65
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65

About This Presentation

No description available for this slideshow.


Slide Content

What is Event? Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page are the activities that causes an event to happen .

Types of Event Foreground Events  - Those events which require the direct interaction of user. They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page etc. Background Events  - Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.

What is Event Handling? Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events. Let's have a brief introduction to this model.

The Delegation Event Model has the following key participants namely: Source  - The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide as with classes for source object. Listener  - It is also known as event handler. Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns.

Steps involved in event handling The User clicks the button and the event is generated. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. Event object is forwarded to the method of registered listener class. the method is now get executed and returns.

1.Action Keyword This interface is used for receiving the action event. This interface defines the actionPerformed () method that is invoked when an action event occurs. Method: void actionPerformed ( ActionEvent e )

Example of Action Listener public class changePanel implements ActionListener { //Initializing jf,jb,jl1,jl2,jp1,jp2… changePanel (){ jf =new JFrame ("Presentation"); jb =new JButton ("click"); jl1=new JLabel ("1st PANEL"); jl2=new JLabel ("2nd PANEL"); jp1=new JPanel (); jp2=new JPanel (); jf.setSize (500,500); jf.setBackground ( Color.LIGHT_GRAY ); jf.setLayout (new FlowLayout ()); jf.add ( jb ); jp1.setSize(200,200); jf.add (jp1); jp1.add(jl1); jp2.add(jl2); jf.setVisible (true);

Conti…. jb.addActionListener (this); } public void actionPerformed ( ActionEvent ae ){ if( ae.getSource ()== jb ){ jp1.setVisible(false); jf.add (jp2); jp2.add(jl2); } } }

Output Before clicking the button

Output after clicking the button

2.Item Listener This interface is used for receiving the item events. Method: void itemStateChanged ( ItemEvent e)

Example… //where initialization occurs checkbox.addItemListener (this); ... public void itemStateChanged ( ItemEvent e) { if ( e.getStateChange () == ItemEvent.SELECTED ) { label.setVisible (true); ... } else { label.setVisible (false); } }

3.KeyEvent class This interface is used for receiving the key events. Methods: public void keyPressed ( KeyEvent e) The "key pressed" event. This event is generated when a key is pushed down . public void keyReleased ( KeyEvent e) The "key released" event. This event is generated when a key is let up. public void keyTyped ( KeyEvent e) The "key typed" event. This event is generated when a character is entered.

Example….. Package presentation; import java.awt.*; Import java.awt.event .*; Public class keyEventDemo implements KeyListeners { //initialization occur of jf,jlabel,jText ; //memory allocation to them jf.setSize (500,500); jf.setBackground ( Color.LIGHT_GRAY );

Conti…. jf.setLayout (null); jt1.setBounds(400,200,180,30); jf.add (jt1); jl1.setBounds(100,200,200,30); jf.add ( jl jt1.addKeyListener(this); jf.setVisible (true); } public void keyTyped ( KeyEvent e) { jl1.setText("key is typed"); }

Conti… public void keyPressed ( KeyEvent e) { jl1.setText("key is pressed"); } public void keyReleased ( KeyEvent e) { jl1.setText("key is relesed "); } }

Output of the example is.. No interface method is active yet

Output after typing “n”….

Output after releasing “n”…

Output after pressing shift..

4.Mouse Listener This interface is used for receiving the mouse event. Methods: public void mouseClicked ( MouseEvent e) {…} Called just after the user clicks the listened-to component. public void mousePressed ( MouseEvent e) {….} Called just after the user presses a mouse button while the cursor is over the listened-to component. public void mouseReleased ( MouseEvent e) {...} Called just after the user releases a mouse button after a mouse press over the listened-to component public void mouseEntered ( MouseEvent e) {…….} Called just after the cursor enters the bounds of the listened-to component. public void mouseExited ( MouseEvent e) {……..} Called just after the cursor exits the bounds of the listened-to component.

Example of Mouse Listener.. p ackage presentation; Import java.awt.*; Import java.awt.event .*; p ublic class changePanel implements MouseListener { //initialization of varibles occur //memory is allocated

jf.setSize (500,500); jf.setBackground ( Color.LIGHT_GRAY ); jf.setLayout (null); jt1.setBounds(300,150,180,30); jf.add (jt1); jl1.setBounds(100,150,200,30); jf.add (jl1); jf.setVisible (true); jt1.addMouseListener(this); } public void mouseClicked ( MouseEvent e) { jl1.setText("Mouse is clicked"); } public void mousePressed ( MouseEvent e) { jl1.setText("Mouse is Pressed"); }

Example conti ….. public void mouseReleased ( MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseEntered ( MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseExited ( MouseEvent e) { jl1.setText("Mouse is Exited"); } }

Output of the example.. When no interface method is called ..

Output of the example … when mouse is entered in the text field…..

Output of the example … when mouse is Exited in the text field…..

Output of the example … when mouse is Clicked in the text field…..

Output of the example … when mouse is Pressed in the text field…..

Output of the example … when mouse is Released in the text field…..

5.TextListener interface The listener interface for receiving text events. Method: public void textValueChanged ( TextEvent  e) Invoked when the value of the text has changed. The code written for this method performs the operations that need to occur when text changes.

6.WindowListener interface This interface is used for receiving the window events. Method: void windowActivated ( WindowEvent  e) Invoked when the Window is set to be the active Window. void windowDeactivated ( WindowEvent  e) Invoked when a Window is no longer the active Window. void windowClosed ( WindowEvent  e) Invoked when a window has been closed as the result of calling dispose on the window void windowClosing ( WindowEvent  e) Invoked when the user attempts to close the window from the window's system menu. void windowIconified ( WindowEvent  e) Invoked when a window is changed from a normal to a minimized state. For many platforms, a minimized window is displayed as the icon specified in the window's iconImage property. void windowDeiconified ( WindowEvent  e) Invoked when a window is changed from a minimized to a normal state. void windowOpened ( WindowEvent  e) Invoked the first time a window is made visible.

7.ContainerListener interface The listener interface is used for receiving container events. Method: void componentAdded ( ContainerEvent  e) Invoked when a component has been added to the container. void componentRemoved ( ContainerEvent  e) Invoked when a component has been removed from the container.

8.MouseMotion Listener Interface This interface is used for receiving mouse motion events. Methods: public void mouseDragged ( MouseEvent e) It invokes when a mouse button is pressed on a component and then dragged. public void mouseMoved ( MouseEvent e) It invokes when a mouse cursor has been moved on to a component but no button has been pushed.

Example …. Package presentation; import java.awt.*; Import java.awt.event .*; Public class mouseMotionDemo implements MouseMotionListeners { //initialization occur of jf,jlabel,jText ; //memory allocation to them jf.setSize (500,500); jf.setBackground ( Color.LIGHT_GRAY );

jf.setLayout (null); jt1.setBounds(300,150,180,30); jf.add (jt1); jl1.setBounds(100,150,200,30); jf.add (jl1); jf.setVisible (true); jt1.addMouseMotionListener(this); } public void mouseDragged ( MouseEvent e) { jl1.setText("Mouse is dragged"); } public void mouseMoved ( MouseEvent e) { jl1.setText(“ Mouse is Moved"); }}

Output of Example

Output of Example

.

9.Focus Listener Interface This interface is used for receiving the focus events methods. Methods: public void focusGained ( FocusEvent e) It invokes when a component gains the keyboard focus. public void focusLost ( FocusEvent e) It invokes when a component loss the keyboard focus.

Example package presentation5; import java.awt.*; import java.awt.event .*; public class FocusListenertest extends Frame implements FocusListener { //initialization occur… public FocusListenertest () { //Memory allocation add(b1=new Button ("First"),"South"); add(b2=new Button ("Second"),"North"); add(l); b1.addFocusListener(this); b2.addFocusListener(this); setSize (200,200);

Example conti … } public void focusGained ( FocusEvent fe ) { if( fe.getSource ()==b1) { l.setText ("focus gained of first & Lost of second"); } public void focusLost ( FocusEvent fe ) { if( fe.getSource ()==b1) { l.setText ("focus Lost of First & Gained of Second "); }

Output of the Example.. When Button First is pressed…

Output of the Example.. When second is pressed…

Adapter Class Adapters are used to replace the listeners. Adapter make event handling easy to the programmer. Every listener that includes more than one abstract method has got a corresponding adapter class. The advantage of this adapter is that we can override any one or two methods like instead of all. In case of a listener, we must override all the abstract methods.

Example: The MouseListener interface has five methods: mouseClicked (), mouseEntered (), mouseExited (), mousePressed () and mouseReleased (). If in your program, you just need two events: mouseEntered () and mouseExited () that time you can use adapter class for the mouseListener interface. The adpter classes contain an empty implementation for each method of the event listener interface. To use the adapter class, you have to extend that adapter class.

awt Adapters

EXAMPLE: import java.awt.*; import java.awt.event .*; public class keyadapterdemo { Frame fr ; Label l1; TextField t1; public keyadapterdemo () { fr =new Frame("Key Adapter Example"); l1=new Label("Press button"); t1=new TextField (); fr.setSize (600,400); fr.setLayout (null);   l1.setBounds(50,100,200,30); t1.setBounds(300,100,200,30); fr.add (l1); fr.add (t1); t1.addKeyListener(new KeyAdapter () { public void keyTyped ( KeyEvent e) { System.out.println ("you pressed "+ e.getKeyChar ()); } } );

OUTPUT

Inner Class Inner classes are class within class. Inner class has special relationship with other class. The special relationship gives inner class access to method of outer class as if they are the part of outer class. Inner class instants or object has access to all method of outer class (public, private and protected). As with instants methods and variables, an inner class is associated with an instants of its enclosing class and has direct access to that object methods and field also an inner class is associated with an instances it can not define any static members itself.

Syntax: Class outerclass { class innerclass { …….. } }

Example Import java.io.*; public class outer { int i =10; p ublic outer(); { n ew inner(); System.out.println (“Inside outer class”); } Class inner { p ublic inner() { System.out.println (“Inside inner class”); System.out.print (“Value of outer class variable i =“+ i ); } } public static void main(String [] args ) { New outer(); } }

So….. this was a little Knowledge about the concept of Event Handling……. Thank You….
Tags