Unit - 3
Unit - 3
Event handling
• 1. Foreground Events
• Foreground events are the events that require user interaction to generate
• 2. Background Events
• Events that don’t require interactions of users to generate are known as
background events.
• Eg. operating system failures/interrupts, operation completion, etc.
• Event Handling
• It is a mechanism to control the events and to decide what should happen
after an event occur.
• To handle the events, Java follows the Delegation Event model.
ActionEvent ActionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
FocusEvent FocusListener
Event Classes Listener Interfaces
ActionListener •actionPerformed()
AdjustmentListener •adjustmentValueChanged()
ItemListener •itemStateChanged()
MouseWheelListener •mouseWheelMoved()
TextListener •textChanged()
• Button
– public void addActionListener(ActionListener a){}
• MenuItem
– public void addActionListener(ActionListener a){}
• TextField
– public void addActionListener(ActionListener a){}
– public void addTextListener(TextListener a){}
• TextArea
– public void addTextListener(TextListener a){}
• Checkbox
– public void addItemListener(ItemListener a){}
• Choice
– public void addItemListener(ItemListener a){}
• List
– public void addActionListener(ActionListener a){}
– public void addItemListener(ItemListener a){}
Java Event Handling Code
• We can put the event handling code into one of the following places:
1. Within class
2. Other class
3. Anonymous class
Within a class
import java.awt.*;
import java.awt.event.*;
AEvent() {
tf = new TextField();
tf.setBounds(60, 50, 170, 20);
Button b = new Button("Click me");
b.setBounds(100, 120, 80, 30);
add(b);
add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.RED);
g.fillOval(e.getX(),e.getY(),20,20);
}
Method Description
public abstract void windowActivated (WindowEvent e); It is called when the Window is set to be an active Window.
public abstract void windowClosed (WindowEvent e); It is called when a window has been closed as the result of calling
dispose on the window.
public abstract void windowClosing (WindowEvent e); It is called when the user attempts to close the window from the
system menu of the window.
public abstract void windowDeactivated (WindowEvent e); It is called when a Window is not an active Window anymore.
public abstract void windowDeiconified (WindowEvent e); It is called when a window is changed from a minimized to a normal
state.
public abstract void windowIconified (WindowEvent e); It is called when a window is changed from a normal to a minimized
state.
public abstract void windowOpened (WindowEvent e); It is called when window is made visible for the first time.
public class WindowExample extends Frame implements WindowListener {
WindowExample() {
addWindowListener(this);
setSize (400, 400);
setLayout (null);
setVisible (true);
}
public static void main(String[] args) { new WindowExample(); }
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
• Advantage of Java inner classes
class Inner {
class Main {
class GFG {
public static void main(String[] args)
{
Outer x = new Outer();
x.outerMethod();
}
}
• The End