what is event

65
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.

Upload: asmita-prasad

Post on 10-Feb-2017

40 views

Category:

Documents


0 download

TRANSCRIPT

What is Event?

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 Listenerpublic class changePanel implements ActionListener{//Initializing jf,jb,jl1,jl2,jp1,jp2changePanel(){ 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 ListenerThis 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 ListenerThis 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..package presentation;Import java.awt.*;Import java.awt.event.*;public 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(TextEvente)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(WindowEvente)Invoked when the Window is set to be the active Window. void windowDeactivated(WindowEvente)Invoked when a Window is no longer the active Window. void windowClosed(WindowEvente)Invoked when a window has been closed as the result of calling dispose on the windowvoid windowClosing(WindowEvente)Invoked when the user attempts to close the window from the window's system menu. void windowIconified(WindowEvente)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(WindowEvente)Invoked when a window is changed from a minimized to a normal state. void windowOpened(WindowEvente)Invoked the first time a window is made visible.

7.ContainerListener interfaceThe listener interface is used for receiving container events.Method:void componentAdded(ContainerEvente)Invoked when a component has been added to the container.

void componentRemoved (ContainerEvente)Invoked when a component has been removed from the container.

8.MouseMotion Listener InterfaceThis 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 InterfaceThis 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.

Examplepackage 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 ClassAdapters 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

Eimport 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());}});fr.setVisible(true);}

public static void main(String []args){ new keyadapterdemo();}}XAMPLE:

OUTPUT

Inner ClassInner 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{..}}

ExampleImport java.io.*;public class outer{int i=10;public outer();{new inner();System.out.println(Inside outer class);}Class inner{public 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.