What are the differences between an event listener interface and an event adapter class in Java?


An EventListener interface defines the methods that must be implemented by an event handler for a particular kind of an event whereas an Event Adapter class provides a default implementation of an EventListener interface.

Event Listener

  • The Event Listeners are the backbone of every component to handle the events.
  • Every method of a particular EventListener will have a single parameter as an instance which is the subclass of EventObject class.
  • An EventListener interface needs to be extended and it will be defined in java.util package.
  • A few EventListener interfaces are ActionListener, KeyListener, MouseListener, FocusListener, ItemListener and etc.

Example

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

public class KeyListenerTest implements KeyListener, ActionListener {
   JFrame frame;
   JTextField tf;
   JLabel lbl;
   JButton btn;
   public KeyListenerTest() {
      frame = new JFrame();
      lbl = new JLabel();
      tf = new JTextField(15);
      tf.addKeyListener(this);
      btn = new JButton("Clear");
      btn.addActionListener(this);
      JPanel panel = new JPanel();
      panel.add(tf);
      panel.add(btn);

      frame.setLayout(new BorderLayout());
      frame.add(lbl, BorderLayout.NORTH);
      frame.add(panel, BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
   @Override
   public void keyTyped(KeyEvent ke) {
      lbl.setText("You have typed "+ ke.getKeyChar());
   }
   @Override
   public void keyPressed(KeyEvent ke) {
      lbl.setText("You have pressed "+ ke.getKeyChar());
   }
   @Override
   public void keyReleased(KeyEvent ke) {
      lbl.setText("You have released "+ ke.getKeyChar());
   }
   @Override
   public void actionPerformed(ActionEvent ae) {
      tf.setText("");
   }
   public static void main(String args[]) {
      new KeyListenerTest();
   }
}

Output

Event Adapter

  • The Abstract classes can be called as Event Adapter for receiving various events.
  • An Event Adapter class gives a default implementation of all the methods in an EventListener interface.
  • Few Event Adapter classes are FocusAdapter, KeyAdapter, MouseAdapter, WindowAdapter and etc.

Example

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

public class KeyAdapterTest {
   private JFrame frame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;
   public KeyAdapterTest() {
      initializeUI();
   }
   private void initializeUI() {
      frame = new JFrame("KeyAdapter class");
      frame.setSize(350, 275);
      frame.setLayout(new GridLayout(3, 1));
      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);
      msgLabel.setSize(300, 100);

      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      frame.add(headLabel);
      frame.add(controlPanel);
      frame.add(msgLabel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   private void showMouseApapter() {
      headLabel.setText("KeyAdapter Test");
      final JTextField textField = new JTextField(10);
      JButton displayButton = new JButton("Display");
      displayButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            msgLabel.setText("You entered : " + textField.getText());
         }
      });
      textField.addKeyListener(new KeyAdapter() {
         public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
               msgLabel.setText("You entered : " + textField.getText());
            }
         }
      });
      controlPanel.add(textField);
      controlPanel.add(displayButton);
      frame.setVisible(true);
   }
   public static void main(String[] args) {
      KeyAdapterTest test = new KeyAdapterTest();
      test.showMouseApapter();
   }
}

Output

raja
Published on 10-Jul-2019 21:31:06
Advertisements