Professional Documents
Culture Documents
Object Oriented Programming - GUI
Object Oriented Programming - GUI
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
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;
ActionEvent ContainerEvent
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