KEMBAR78
Event Handling in java | PPTX
EventHandlingIn Java……..
Some common term used in EventHandlingEvent :          An event is an Object that describes a                state change in a Source.     * Some of the activities that causes event to be generated are :                   *Pressing a Button.                    *Entering a character through                             Key Board.                     *Selecting an item form a list etc.
Some Common term Conti..Event Source :         A source is an object that generates an event.   Some general Event Sources are:                      #Button, CheckBox                      #List, MenusItem                      #Window, TextItems Etc…Here is a general form for adding a listener to an event Source :          public void addTypeListener(TypeEvent e)        * Type is the name of the event.         * e is the reference of the event listener.
Some common terms Conti…Event Listener :              A Listener is an object that is notified when an event occurs.             For example :MouseMotionListener interface define two events:                 *When mouse is dragged.                  * When mouse is moved.
Some common terms Conti…For implementing event listener we have to import the following Statement:                      import java.awt.event.*;
Event HandlingThere are a number of event class provided by java :  But we’ll discuss today only 7 classes, namely:              *ActionEvent               *KeyEvent               *MouseEvent               *MouseMotionEvent               *FocusEvent               *WindowEvent                *ItemEvent
Action Event ClassAction listeners are probably the easiest — and most common — event handlers to implement.To write an Action Listener, follow the steps given below:Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface.      For example:                     public class MyClass implements ActionListener {                            }
Actionevent Listener conti…..Register an instance of the event handler class as a listener on one or more components.         For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface.          For example:                       public void actionPerformed(ActionEvent e)                                     {                                             if(e.getSource()==someComponent)                                        //code that reacts to the action...                                     }
Example of Action Listenerpublic 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(ActionEventae){        if(ae.getSource()==jb){           jp1.setVisible(false);jf.add(jp2);            jp2.add(jl2);             }    }}
Output Before clicking the button
Output after clicking the button
Key Event ClassThis class has 3 methods in Listener interfaceNamely:       public void keyTyped(KeyEventae)           {//active on typing a code…..}       public void keyPressed(KeyEventae)        {//active on pressing a key……}        public void keyReleased(KeyEventae)         {//active on realesing a key…..}
Example…..Package presentation;import java.awt.*;Import java.awt.event.*;Public class keyEventDemo implements KeyListeners{//initialization occur of jf,jlabel,jText;//memory allocation to themjf.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..
Mouse Event ClassThis class has 5 interface method as follows :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…..
MouseMotionEvent ClassThis class provides  2 interface methods:          *mouseDragged Method:                 executed when mouse is dragged over the listened-to component..       *mouseMoved Method:             executed when mouse is moved over the listened-to component…
Example ….Package presentation;import java.awt.*;Import java.awt.event.*;Public class mouseMotionDemo implements MouseMotionListeners{//initialization occur of jf,jlabel,jText;//memory allocation to themjf.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
FocusEvent Listener ClassThis class provide two interface methods:focusGained:       Called just after the listened-to component gets the focus. focusLost:Called just after the listened-to component Loses the 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(FocusEventfe)  {         if(fe.getSource()==b1)        {l.setText("focus gained of first & Lost of second");} public void focusLost(FocusEventfe) {       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…
WindowEvent ClassThis  class provide 8 interface methods:  # windowOpened:Called just after the listened-to window has been shown for the first time.#windowClosing:             Called in response to a user request for the listened-to window to be closed. To actually close the window, the listener should invoke the window's dispose or setVisible(false) method.
WindowEvent Class conti…windowClosed:             Called just after the listened-to window has closed. windowIconified:            Called just after the listened-to window is iconified .windowDeicoified: Called just after the listened-to window is deiconified.
WindowEvent Class conti…windowActivated and windowDeactivated :           Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus andwindowLostFocus methods to determine when a window gains or loses the focus.
ItemEvent ClassThis class provide one interface method:itemStateChanged:                 Called just after a state change in the     listened-to component.
Example…//where initialization occurscheckbox.addItemListener(this); ...  public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { label.setVisible(true); ... } else { label.setVisible(false); } }
  So….. this was a little Knowledge about the  concept of Event Handling…….      Thank You….

Event Handling in java

  • 1.
  • 2.
    Some common termused in EventHandlingEvent : An event is an Object that describes a state change in a Source. * Some of the activities that causes event to be generated are : *Pressing a Button. *Entering a character through Key Board. *Selecting an item form a list etc.
  • 3.
    Some Common termConti..Event Source : A source is an object that generates an event. Some general Event Sources are: #Button, CheckBox #List, MenusItem #Window, TextItems Etc…Here is a general form for adding a listener to an event Source : public void addTypeListener(TypeEvent e) * Type is the name of the event. * e is the reference of the event listener.
  • 4.
    Some common termsConti…Event Listener : A Listener is an object that is notified when an event occurs. For example :MouseMotionListener interface define two events: *When mouse is dragged. * When mouse is moved.
  • 5.
    Some common termsConti…For implementing event listener we have to import the following Statement: import java.awt.event.*;
  • 6.
    Event HandlingThere area number of event class provided by java : But we’ll discuss today only 7 classes, namely: *ActionEvent *KeyEvent *MouseEvent *MouseMotionEvent *FocusEvent *WindowEvent *ItemEvent
  • 7.
    Action Event ClassActionlisteners are probably the easiest — and most common — event handlers to implement.To write an Action Listener, follow the steps given below:Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { }
  • 8.
    Actionevent Listener conti…..Registeran instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { if(e.getSource()==someComponent) //code that reacts to the action... }
  • 9.
    Example of ActionListenerpublic 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);
  • 10.
    Conti….jb.addActionListener(this); } public void actionPerformed(ActionEventae){ if(ae.getSource()==jb){ jp1.setVisible(false);jf.add(jp2); jp2.add(jl2); } }}
  • 11.
  • 12.
  • 13.
    Key Event ClassThisclass has 3 methods in Listener interfaceNamely: public void keyTyped(KeyEventae) {//active on typing a code…..} public void keyPressed(KeyEventae) {//active on pressing a key……} public void keyReleased(KeyEventae) {//active on realesing a key…..}
  • 14.
    Example…..Package presentation;import java.awt.*;Importjava.awt.event.*;Public class keyEventDemo implements KeyListeners{//initialization occur of jf,jlabel,jText;//memory allocation to themjf.setSize(500,500);jf.setBackground(Color.LIGHT_GRAY);
  • 15.
    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"); }
  • 16.
    Conti… public voidkeyPressed(KeyEvent e) { jl1.setText("key is pressed"); } public void keyReleased(KeyEvent e) { jl1.setText("key is relesed"); }}
  • 17.
    Output of theexample is..No interface method is active yet
  • 18.
  • 19.
  • 20.
  • 21.
    Mouse Event ClassThisclass has 5 interface method as follows :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.
  • 22.
    Example of MouseListener..package presentation;Import java.awt.*;Import java.awt.event.*;public class changePanel implements MouseListener{//initialization of varibles occur//memory is allocated
  • 23.
    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"); }
  • 24.
    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"); }}
  • 25.
    Output of theexample..When no interface method is called..
  • 26.
    Output of theexample …when mouse is entered in the text field…..
  • 27.
    Output of theexample …when mouse is Exited in the text field…..
  • 28.
    Output of theexample …when mouse is Clicked in the text field…..
  • 29.
    Output of theexample …when mouse is Pressed in the text field…..
  • 30.
    Output of theexample …when mouse is Released in the text field…..
  • 31.
    MouseMotionEvent ClassThis classprovides 2 interface methods: *mouseDragged Method: executed when mouse is dragged over the listened-to component.. *mouseMoved Method: executed when mouse is moved over the listened-to component…
  • 32.
    Example ….Package presentation;importjava.awt.*;Import java.awt.event.*;Public class mouseMotionDemo implements MouseMotionListeners{//initialization occur of jf,jlabel,jText;//memory allocation to themjf.setSize(500,500);jf.setBackground(Color.LIGHT_GRAY);
  • 33.
    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"); }}
  • 34.
  • 35.
  • 36.
    FocusEvent Listener ClassThisclass provide two interface methods:focusGained: Called just after the listened-to component gets the focus. focusLost:Called just after the listened-to component Loses the focus.
  • 37.
    Examplepackage presentation5;import java.awt.*;importjava.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);
  • 38.
    Example conti…}public voidfocusGained(FocusEventfe) { if(fe.getSource()==b1) {l.setText("focus gained of first & Lost of second");} public void focusLost(FocusEventfe) { if(fe.getSource()==b1){ l.setText("focus Lost of First & Gained of Second ");}
  • 39.
    Output of theExample..When Button First is pressed…
  • 40.
    Output of theExample..When second is pressed…
  • 41.
    WindowEvent ClassThis class provide 8 interface methods: # windowOpened:Called just after the listened-to window has been shown for the first time.#windowClosing: Called in response to a user request for the listened-to window to be closed. To actually close the window, the listener should invoke the window's dispose or setVisible(false) method.
  • 42.
    WindowEvent Class conti…windowClosed: Called just after the listened-to window has closed. windowIconified: Called just after the listened-to window is iconified .windowDeicoified: Called just after the listened-to window is deiconified.
  • 43.
    WindowEvent Class conti…windowActivatedand windowDeactivated : Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus andwindowLostFocus methods to determine when a window gains or loses the focus.
  • 44.
    ItemEvent ClassThis classprovide one interface method:itemStateChanged: Called just after a state change in the listened-to component.
  • 45.
    Example…//where initialization occurscheckbox.addItemListener(this);... public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { label.setVisible(true); ... } else { label.setVisible(false); } }
  • 46.
    So…..this was a little Knowledge about the concept of Event Handling……. Thank You….