Event Handling
Event Handling
• Listeners: Listeners are used for handling the events generated from the source. Each of these listeners
represents interfaces that are responsible for handling events.
• To perform Event Handling, we need to register the source with the listener.
Syntax:
addTypeListener()
where Type represents the type of event.
Example 1: For KeyEvent we use addKeyListener() to register.
Event Classes in Java
Event Class Listener Interface Description
AdjustmentListener •adjustmentValueChanged()
•componentResized()
•componentShown()
ComponentListener
•componentMoved()
•componentHidden()
•componentAdded()
ContainerListener
•componentRemoved()
•focusGained()
FocusListener
•focusLost()
ItemListener •itemStateChanged()
•keyTyped()
KeyListener •keyPressed()
•keyReleased()
•mousePressed()
•mouseClicked()
MouseListener •mouseEntered()
•mouseExited()
•mouseReleased()
•mouseMoved()
MouseMotionListener
•mouseDragged()
MouseWheelListener •mouseWheelMoved()
TextListener •textChanged()
•windowActivated()
•windowDeactivated()
•windowOpened()
WindowListener •windowClosed()
•windowClosing()
•windowIconified()
•windowDeiconified()
Key Differences: AWT vs Swing
Feature AWT (java.awt) Swing (javax.swing)
Heavyweight (uses native OS
Component Type Lightweight (Java-based)
UI)
Platform Dependency OS-dependent Platform-independent
Customizable with
Look & Feel OS-defined
UIManager
Event Handling Uses old event model Uses event delegation model
Uses
Thread-Safety Not thread-safe
SwingUtilities.invokeLater()
Customization Limited Supports icons, tooltips, etc.
Slower due to OS
Performance Faster and more flexible
dependency
Best Use Case Simple UI applications Modern applications
import java.awt.*; add(b);add(tf);
import java.awt.event.*; setSize(300,300);
class AEvent extends Frame implements ActionList setLayout(null);
ener setVisible(true);
{ }
TextField tf; public void actionPerformed(ActionEvent e)
AEvent() {
{ tf.setText("Welcome");
tf=new TextField(); }
tf.setBounds(60,50,170,20); public static void main(String args[]){
Button b=new Button("click me"); new AEvent();
b.setBounds(100,120,80,30); }
b.addActionListener(this); }
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame()
{
setTitle("Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new JButton("Click Me"));
setVisible(true);
}
public static void main(String[] args)
{
new MyFrame();
}
}
import javax.swing.*;
import java.awt.*;
setLayout(new FlowLayout());
add(new JButton("Click Me"));
add(new JButton("Search"));
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
HANDLING MOUSE EVENTS
/*Write a program to create a text area and display the mouse event when the
the mouse is clicked, when the mouse is moved, etc. is done by the user
Mouse events*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEvents extends JFrame implements MouseListener, MouseMotionListener
{
String str= " ";
JTextArea ta;
Container c;
int x,y;
public MouseEvents()
{ c= getContentPane();
c.setLayout(new FlowLayout());
ta = new JTextArea ("Click the mouse ",5,20);
ta.setFont(new Font("Arial", Font. BOLD, 30));
c.add(ta);
ta.addMouseListener(this);
ta.addMouseMotionListener(this);
}
public void mouseClicked (MouseEvent me)
{
int i= me.getButton();
if(i==1)
str += "Clicked Button: Left";
else if(i==2)
str += "Clicked Button: Middle";
else if(i==3)
str += "Clicked Button: Right";
this.display();
}
public void mouseEntered (MouseEvent me)
{