+92 332 4229 857 99ProjectIdeas@Gmail.com

Event Handling In Java


Event Handling:

GUIs generate events when the user interacts with GUI

For example,
       
        -> Clicking a button
        -> Moving the mouse
        -> Closing Window etc.

IN JAVA, EVENTS ARE REPRESENTED BY OBJECTS:

These objects tell us about event and its source. Examples are:

       -> ActionEvent (Clicking a button)
       -> WindowEvent (Doing something with window e.g. closing , minimizing)

BOTH AWT AND SWING COMPONENTS (NOT ALL) GENERATE EVENTS:

         import java.awt.event.*;
         import javax.swing.event.*;

Code

EVENT HANDLING STEPS:

For a programmer the Event Handling is a three step process in terms of code.


STEP 1:
        Create components which can generate events



STEP 2:
        Build component (objects) that can handle events (Event Handlers)


STEP 3:

Register handlers with generators

EVENT GENERATORS:

    You have already seen a lot of event generators

        -> Buttons
        -> Mouse
        -> Key
        -> Window

JButton helloButton = new JButton("Hello");
       Now, helloButton can generate events.


EVENT HANDLERS/ EVENT LISTENER:
         
-> Java defines interfaces for every event type
-> If a class needs to handle an event. It needs to implement the corresponding listener interface:

       -> To handle “ActionEvent” a class needs to implement “ActionListener”
       -> To handle “KeyEvent” a class needs to implement “KeyListener”
       -> To handle “MouseEvent” a class needs to implement “MouseListener”

REGISTERING HANDLER WITH GENERATOR:
       
  The event generator is told about the object which can handle its events

Event Generators have a method:

        .add______Listener(________)

    helloButton.addActionListener(objectOfTestClass)
    helloButton.addActionListener(this);

Or,

    myFrame.addWindowListener(this);


EXAMPLE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class EventHandling implements ActionListener,KeyListener,WindowListener {

        JFrame myFrame = null;
        Container con = null;
        JButton helloButton = null;
        JTextField inputTextField = null;         

    
     public EventHandling() {
         createComponents();
     }

       public void createComponents(){ // initializing GUI interface

              myFrame = new JFrame();
              helloButton = new JButton("Hello");
              inputTextField = new JTextField();
              inputTextField.setPreferredSize(new Dimension(70, 20));
              con = myFrame.getContentPane();
              con.setLayout(new FlowLayout());
           
              // adding inputTextField and helloButton to container
              con.add(inputTextField);
              con.add(helloButton);
           
              myFrame.setSize(250,100);
              myFrame.setVisible(true);
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           

              // Registering events with helloButton , inputTextField and myFrame
              helloButton.addActionListener(this);
              inputTextField.addKeyListener(this);
              myFrame.addWindowListener(this);                   

      }

       public void actionPerformed(ActionEvent e) {

              // if user press HelloButton then this message is displayed

              if (e.getSource().equals(helloButton)) {

                     JOptionPane.showMessageDialog(null, "Hello Button Is Pressed");

              }
      }

// All Methods of KeyListener interface give implementaion of your required method otherwise leave empty bodies.

// we just want keyPressed method so we give its implementaion only.
       public void keyPressed(KeyEvent e) {

        // at every keypress in inputTextField,message is displayed with the key pressed name
           JOptionPane.showMessageDialog(null, "You pressed " + e.getKeyText(e.getKeyCode()));
      }

       public void keyReleased(KeyEvent e){}

       public void keyTyped(KeyEvent e){}

// All Methods of WindowListener interface give implementaion of your required method otherwise leave empty bodies.

       public void windowClosed(WindowEvent e) {}

       public void windowActivated(WindowEvent e){}

// we just want windowClosing method so we give its implementaion only.

       public void windowClosing(WindowEvent e) {

              // when user close the frame GoodBye!!!! message is displayed

              JOptionPane.showMessageDialog(null, "Good Bye!!!!");
       }

       public void windowDeactivated(WindowEvent e){}

       public void windowDeiconified(WindowEvent e){}

       public void windowIconified(WindowEvent e){}

       public void windowOpened(WindowEvent e){}  

}

public static void main(String[] args) {

        new EventHandling();
}

1 comments:

Anonymous said...

Hi,

I find it very helpful.

Thanx