Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

GUI Event Handling

GUI
GUI – pronounced [góo ee] (controls/elements/widgets)
- is an acronym for graphical user interface
interface is a way to interact something
user interface is the way someone interacts with a program
- presents a user-friendly mechanism for interacting with an
application
• GUIs are event-driven
– A user interaction creates an event
• Common events are clicking a button, typing in a text field, selecting
an item from a menu, closing a window and moving the mouse
– The event causes a call to a method called an event handler
• Researchers at Xerox Palo Alto Research Center (PARC) are
credited with developing the GUI, which was later enhanced by
Apple Computer and then by Microsoft.
• There are two ways in which you can create a GUI for your
program:
– Dialog boxes
– GUI elements or components
• Event-driven programming, code is executed upon activation of
events.
The Delegation Event Model

- Model used by Java to handle user interaction


with GUI components
- Describes how your program can respond to user
Interaction

Three important players


> Event Source
> Event Listener/Handler
> Event Object
Event

Event – occurs when a user takes an action on a


component, such as mouse button clicks, mouse
movements, keystrokes, etc.

Events are responded by event listeners


3 Important Players

1. Event source
- GUI component that generates the event
- Example: button
2. Event Listener/Handler
a. Event listener
– monitors and detects the event and then calls
an event handler
b. Event-handling method(handler method)
– a method that reacts to a specific event
3. Event Object
- Created when an event occurs (i.e., user
interacts with a GUI component)
- Contains all necessary information about the
event that has occurred
- Type of event that has occurred
- Source of the event
Control Flow of Delegation Event Model
1. A source should be registered with a listener
2. Once registered, listener waits until an event occurs
3. When an event occurs
- An event object is created by the event source
- Event object is fired by the event source to the
registered listeners (method of event listener is
called with an event object as an argument)
4. Once the listener receives an event object from the source
- The overriding method processes the event that occurred.
3 Steps for an Event Handler

1. implements a listener
- use implements keyword in the class declaration
example:
public class MyClass implements ActionListener {
2. register your source with listener
someComponent.addActionListener(instanceOfMyClass);
3. implement methods
- declare and fully defined all methods for the interface
that you implement
public void actionPerformed(ActionEvent e) {
//code that reacts to the action...
}
public class JavaApp {
public static void main(String args[]) {
HelloFrame f=new HelloFrame();
}
import java.awt.*;
}
import javax.swing.*;
import java.awt.event.*;
public class HelloFrame extends JFrame implements ActionListener{
public HelloFrame() {
initComponents();
setVisible(true);
}
private void initComponents() {
//---- btnPressMe ----
btnPressMe.addActionListener(this);
}
private JButton btnPressMe;

public void actionPerformed(ActionEvent e){


lblDisplayName.setText("Hello!”);
}
}
Event Classes

ActionEvent ContainerEvent

AdjustmentEvent FocusEvent MouseEvent

EventObject AWTEvent ComponentEvent InputEvent

ItemEvent PaintEvent KeyEvent

TextEvent WindowEvent

ListSelectionEvent

EventObject – java.util
AWTEvent – java.awt
ActionEvent, etc. – java.awt.event
ListSelectionEvent – javax.swing.event
Selected User Actions
Source Event Type
User Action Object Generated

Click a button JButton ActionEvent


Click a check box JCheckBox ItemEvent, ActionEvent
Click a radio button JRadioButton ItemEvent, ActionEvent
Select a new item JComboBox ItemEvent, ActionEvent
Select an item from a List JList ListSelectionEvent
Window opened, closed, etc. Window WindowEvent
Mouse pressed, released, etc. Any Component MouseEvent
Key released, pressed, etc. Any Component KeyEvent
Change a text in a textfield TextField TextEvent
List of Events with their listeners and handlers
Event Type Listener(s) Handler(s)
ActionEvent ActionListener void actionPerformed(ActionEvent e)

ItemEvent ItemListener void itemStateChanged(ItemEvent e)

TextEvent TextListener void textValueChanged(TextEvent e)

KeyEvent KeyListener void keyPressed(KeyEvent e)


void keyTyped(KeyEvent e)
void keyReleased(KeyEvent e)
List of Events with their listeners and handlers

Event Type Listener(s) Handler(s)


MouseEvent MouseListener void mousePressed(MouseEvent e)
MouseMotionListener void mouseReleased(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mouseClicked(MouseEvent e)
void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)

ListSelection ListSelectionListener void valueChanged(ListSelectionEvent e)


Event
END

You might also like