+92 332 4229 857 99ProjectIdeas@Gmail.com

Event Handling Using Inner Classes (Java)


Event Handling Using Inner Classes (Java):

Generally we define inner classes for event handling, and by convention we define the name as for example if to handle buttons events you would define ButtonHandler, likewise same if handling window/frame events should define class WindowHandler etc. We define inner classes private.

Code 
 
First creating an GUI interface which contains a button, Label and obviously a window.

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

public class EventHandling {
      
         JFrame myFrame = null;
         Container con = null;
         JButton startButton = null;
         JLabel mousePositionLabel = null;
      
   public EventHandling() {
               
        createComponents();
        
   }

  public void createComponents(){ //initializing GUI interface

       myFrame = new JFrame();
       startButton = new JButton("Hello");
       mousePositionLabel = new JLabel("Mouse Position");
             
       con = myFrame.getContentPane();
       con.setLayout(new FlowLayout());
      
       con.add(startButton);
       con.add(mousePositionLabel);
             
       myFrame.setSize(250,200);
       myFrame.setVisible(true);
       myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
Here, you see creating inner class ButtonHandler object and registering it with startButton:         
       // ButtonHandler inner class object
       ButtonHandler bHandler = new ButtonHandler();

       // Registering ButtonHanlder inner class object with startButton
       startButton.addActionListener(bHandler);

Here, you see creating inner class WindowHandler object and registering it with myframe:             

       // WindowHandler inner class object
       WindowHandler wHandler = new WindowHandler();
      
       // Registering WindowHanlder inner class object with myFrame
       myFrame.addWindowListener(wHandler);

Here, you see creating inner class MouseMotionHandler object and registering it with myframe because when you enter in frame/window it shows you the x and y-axis of mouse movement:
             
       // MouseMotionHandler inner class object
       MouseMotionHandler mmHandler = new MouseMotionHandler();
        
       // Registering MouseMotionHanlder inner class object with myFrame to show mouse pointer (x-axis and y-axis) movement on Frame
       myFrame.addMouseMotionListener(mmHandler);
             
  }

Now, creating inner classes for handling buttons, window events:

// inner class for handling button events
 private class ButtonHandler implements ActionListener {
  
     public void actionPerformed(ActionEvent e) {
    
        JOptionPane.showMessageDialog(null, "I Am From ButtonHandler Class");
     }
 }
        

// inner class for handling window (Frame) events
 private class WindowHandler extends WindowAdapter {
      
    public void windowClosing(WindowEvent we) {
         
        JOptionPane.showMessageDialog(null, "I Am From WindowHandler Class");
    }
 }
     
// inner class for handling Mouse Motion events on Frame
 private class MouseMotionHandler extends MouseMotionAdapter {
      
    public void mouseMoved(MouseEvent me) {
         
       mousePositionLabel.setText("x-axis : " + me.getX() + " y-axis : " + me.getY());
    }
 }

}

0 comments: