Question: List any six Event Listener interface

Answer:

In Java, Event Listener interfaces are used to handle various types of events generated by user interactions or system actions. These interfaces define methods that are called when specific events occur. Here are six commonly used Event Listener interfaces in Java:

1. ActionListener:

  • Used to handle action events, such as button clicks or menu selections.
  • Contains the actionPerformed(ActionEvent e) method.

Example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonClickListener {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Button Example");
        JButton button = new JButton("Click Me");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button clicked!");
            }
        });
        frame.add(button);
        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

2. MouseListener:

  • Used to handle mouse events, such as mouse clicks or movements.
  • Contains methods like mouseClicked(MouseEvent e), mousePressed(MouseEvent e), mouseReleased(MouseEvent e), mouseEntered(MouseEvent e), and mouseExited(MouseEvent e).

3. KeyListener:

  • Used to handle keyboard events, such as key presses or releases.
  • Contains methods like keyTyped(KeyEvent e), keyPressed(KeyEvent e), and keyReleased(KeyEvent e).

4. WindowListener:

  • Used to handle window-related events, such as window opening, closing, activation, or deactivation.
  • Contains methods like windowOpened(WindowEvent e), windowClosing(WindowEvent e), windowClosed(WindowEvent e), windowIconified(WindowEvent e), windowDeiconified(WindowEvent e), windowActivated(WindowEvent e), and windowDeactivated(WindowEvent e).

5. FocusListener:

  • Used to handle focus-related events, such as gaining or losing focus.
  • Contains methods like focusGained(FocusEvent e) and focusLost(FocusEvent e).

6. ItemListener:

  • Used to handle item events, such as selection changes in checkboxes or combo boxes.
  • Contains the itemStateChanged(ItemEvent e) method.

Example:

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CheckBoxListener {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Checkbox Example");
        JPanel panel = new JPanel();
        JCheckBox checkBox = new JCheckBox("Check Me");
        checkBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    System.out.println("Checkbox checked!");
                } else {
                    System.out.println("Checkbox unchecked!");
                }
            }
        });
        panel.add(checkBox);
        frame.add(panel);
        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Conclusion:

Event Listener interfaces in Java provide a mechanism for handling various types of events generated by user interactions or system actions. By implementing these interfaces and overriding their methods, developers can respond to specific events in their Java applications.

Leave a Comment