Anda di halaman 1dari 6

Typical command line program

Non-interactive program:
GUI Event Handling
„

main()
{
„ Linear execution code;
code;
code;
code;
code;
code;
code;
code;
code;
code;
code;
} 2

Interactive command line program Typical GUI program


program:
GUI program:
„ User input commands main()
{
„ User input commands
decl data storage; main()
initialization code; {
decl data storage;
„ Non-linear execution loop
{
„ Non-linear execution initialization code;
create GUI;
„ Unpredictable order get command;
switch(command) „ Unpredictable order register callbacks;
main event loop;
}
Much idle time Much idle time
{
„ command1: „
code; Callback1() //button1 press
command2: { code;
code; }

}

„ Event callback procs Callback2()
{ code;
} }
} …
3 4

What is an Event? Handling Events


„ GUI components communicate with the „ Every time the user types a character or
rest of the applications through events. clicks the mouse, an event occurs.
„ The source of an event is the component „ Any object can be notified of any particular
that causes that event to occur. event.
„ The listener of an event is an object that „ To be notified for an event,
receives the event and processes it ƒ The object has to be registered as an event
appropriately. listener on the appropriate event source.
ƒ The object has to implement the appropriate
interface.

5 6

1
An example of Event Handling The Event Handling process

public class SwingApplication implements When an event is triggered, the JAVA


ActionListener {
runtime first determines its source and type.
...
JButton button = new JButton("I'm a Swing If a listener for this type of event is
button!"); registered with the source, an event object is
button.addActionListener(this); created.
.... For each listener to this type of an event, the
JAVA runtime invokes the appropriate event
public void actionPerformed(ActionEvent e)
{ numClicks++;
label.setText(labelPrefix + numClicks); handling method to the listener and passes
} the event object as the parameter.
}
7 8

The Event Handling Process (2)


What does an Event Handler require?
„ It just looks for 3 pieces of code!
„ First, in the declaration of the event
handler class, one line of code must specify
that the class implements either a listener
interface or extends a class that
implements a listener interface.
public class DemoClass implements
ActionListener {

9 10

What does an Event Handler require? (3)


What does an Event Handler require? (2)

„ Second, it looks for a line of code which „ Third, the event handler must have a piece
registers an instance of the event handler of code that implements the methods in the
class as a listener of one or more listener interface.
components because, as mentioned earlier,
the object must be registered as an event public void actionPerformed(ActionEvent e)
listener. {
… //code that reacts to the action …
anyComponent.addActionListener(instance
Of DemoClass); // if e.source()==button1 …
}

11 12

2
Types of Events Types of Events (2)
„ Below, are some of the many kinds of events,
swing components generate. Act causing Event Listener Type

Act causing Event Listener Type User moving the mouse MouseMotionListener
over a component
User clicks a button, presses ActionListener
Enter, typing in text field Component becomes ComponentListener
User closes a frame WindowListener visible

Clicking a mouse button, while MouseListener Table or list selection ListSelectionListener


the cursor is over a changes
component
13 14

The Event classes Event Listeners


„ An event object has an event class as „ Event listeners are the classes that
its reference data type. implement the
<type>Listener interfaces.
„ The Event object class
ƒ Defined in the java.util package. Example:
„ The AWT Event class 1. ActionListener receives action events
ƒ An immediate subclass of EventObject. 2. MouseListener receives mouse events.
ƒ Defined in java.awt package. The following slides give you a brief
ƒ Root of all AWT based events. overview on some of the listener types.
15 16

The ActionListener Method The MouseListener Methods


„ It contains exactly one method. „ Event handling when the mouse is
clicked.
Example:
public void mouseClicked(MouseEvent e)
„ Event handling when the mouse enters a
component.
public void actionPerformed(ActionEvent e)
The above code contains the handler public void mouseEntered(MouseEvent e)
for the ActionEvent e that occurred. „ Event handling when the mouse exits a
component.
public void mouseExited(MouseEvent e)
17 18

3
The MouseListener Methods (2) The MouseMotionListener Methods

„ Event handling when the mouse button „ Invoked when the mouse button is pressed
is pressed on a component. over a component and dragged. Called
public void mousePressed(MouseEvent e) several times as the mouse is dragged
„ Event handling when the mouse button public void mouseDragged(MouseEvent e)
is released on a component. „ Invoked when the mouse cursor has been
public void mouseReleased(MouseEvent e) moved onto a component but no buttons
have been pushed.
public void mouseMoved(MouseEvent e)

19 20

The WindowListener Methods The WindowListener Methods (2)


„ Invoked when the window object is opened.
„ Invoked when the window is set to be the active
public void windowOpened(WindowEvent e) window.
„ Invoked when the user attempts to close public void windowActivated(WindowEvent e)
the window object from the object’s system „ Invoked when the window object is no longer the
menu. active window
public void windowClosing(WindowEvent e) public void windowDeactivated(WindowEvent e)
Invoked when the window is minimized.
Invoked when the window object is closed as
„
„
public void windowIconified(WindowEvent e)
a result of calling dispose (release of Invoked when the window is changed from the
resources used by the source).
„
minimized state to the normal state.
public void windowClosed(WindowEvent e) public void windowDeconified(WindowEvent e)

21 22

Hierarchy of event objects


Additional Listener Types
Note: The number of
event objects is much „ Change Listener „ Item Listener
greater then specified „ Container Listener „ Key Listener
in the diagram. Document Listener
„
„ Property Change Listener
Only some of them are Focus Listener
represented in the
„
„ Table Model Listener
„ Internal Frame Listener
figure

The main purpose of the last few slides is to


give you an idea as to how you can use event
handlers in your programs. See the JAVA
tutorials for more information.
Courtesy: Safari.oreilly.com
23 24

4
Adapter classes for Event
Handling. Adapter classes - Illustration.
„ Why do you need adapter classes? „ You want create a class that implements a
ƒ Implementing all the methods of an interface MouseListener interface, where you
involves a lot of work. require only a couple of methods to be
ƒ If you are interested in only using some methods implemented. If your class directly
of the interface. implements the MouseListener, you must
„ Adapter classes implement all five methods of this
ƒ Built-in in JAVA interface.
ƒ Implement all the methods of each listener „ Methods for those events you don't care
interface with more than one method. about can have empty bodies
ƒ Implementation of all empty methods
25 26

Illustration (2) Illustration (3)


public class MyClass implements MouseListener {
... someObject.addMouseListener(this); „ What is the result?
/* Empty method definition. */ ƒ The resulting collection of empty bodies can
public void mousePressed(MouseEvent e) { }
make the code harder to read and maintain.
/* Empty method definition. */
public void mouseReleased(MouseEvent e) { } „ To help you avoid implementing empty
/* Empty method definition. */
public void mouseEntered(MouseEvent e) { }
bodies, the API generally includes an
/* Empty method definition. */ adapter class for each listener
public void mouseExited(MouseEvent e) { } interface with more than one method.
For example, the MouseAdapter class
public void mouseClicked(MouseEvent e) {
//Event listener implementation goes here...
} implements the MouseListener
}
interface.
27 28

Using Inner classes for Event


How to use an Adapter class? Handling
/* Using an adapter class */ „ Consider that you want to use an
public class MyClass extends MouseAdapter { adapter class but you don’t want your
.... public class to inherit from the adapter
someObject.addMouseListener(this); class.
.... „ For example, you write an applet with
public void mouseClicked(MouseEvent e) { some code to handle mouse events. As
...//Event listener implementation goes
you know, JAVA does not permit
multiple inheritance and hence your
// here
}
}
class cannot extend both the Applet
and MouseAdapter classes.

29 30

5
Creating GUI applications
Using Inner classes (contd..) with Event Handling.
„ Use a class inside your Applet subclass that „ Guidelines:
extends the MouseAdapter class.
1. Create a GUI class
public class MyClass extends Applet { ... ƒ Describes the appearance of your
someObject.addMouseListener(new
MyAdapter());
GUI application.
... 2. Create a class implementing the
class MyAdapter extends MouseAdapter {
void mouseClicked(MouseEvent e) {
public
appropriate listener interface
//Event listener implementation here... } ƒ May refer to the same class as
} step 1.
}
31 32

Creating GUI applications with Creating GUI applications with


Event Handling (contd..) Event Handling (contd..)

4. Register the listener object with


3. In the implementing class the source
Override all methods of the
The object is an instantiation of the
ƒ

appropriate listener interface. ƒ

listener class specified in step 2.


ƒ Describe in each method how you
want to handle the events. ƒ Use the add<Type>Listener method.
ƒ May give empty implementations
for the methods you don’t need.

33 34

Design Considerations Design Considerations


„ The most important rule to keep in mind „ You can have choices on how the event listener
about event listeners is that they must has to be implemented. One particular solution
execute quickly. Since all drawing and event- might not fit in all situations.
listening methods are executed in the same For example, you might choose to implement
thread, a slow event listener might make the separate classes for different types of
program seem unresponsive. So, consider listeners. This might be a relatively easy
the performance issues also when you architecture to maintain, but many classes can
create event handlers in your programs. also result in reduced performance .

35 36