Title: CS4812Fall 98 Java for the Impatient
1CS4812--Fall 98 Java for the Impatient
- A Simplified View of the JDK 1.1
- Event Model
2Administrative Announcements
- P2 due date pushed back. Note changes in
requirements. - Think about your presentation topics, etc. If
you dont have a topic, meet after class for
suggestions.
If you havent yet selected a date, you will be
assigned one.
3The Java Event Model What?
- JDK 1.1 event model gives you four basic event
models to choose from - Listener Interfaces
- Adapter Classes
- Semantic Events
- Use of JDK 1.02s event model
4Overview
- All events begin with the java.util package,
which has a useful class and interface - EventObject a class there merely tracks the
source of the event - EventListener a tagging interface from which all
interfaces extend - The AWT event classes keep track of event
information, whether or not the event is consumed
5Events and Components
- Most AWT components are equipped with an
addXYZListener method that takes an
ActionListener argument. - Since this method anchors from the
java.awt.Component class, the component
extensions (e.g., Buttons, TextFields), can
support component, focus, key, mouse and mouse
motion events. - Components can be registered with only one event
listener at a time however, the listener object
can handle a variety of events.
6Events and Multiple Listenings
- If a component has a listener handling a variety
of events (e.g., mouse and mouse motion) there is
no way to determine which event will be delivered
first. (This stems from the threaded nature of
the JDK 1.1 event queue.) - If the priority of events is important (e.g., a
rubber banding operation), the JDK 1.02 event
model can be used. Which raises another
important point . . .
7Mixing Event Models
- DO NOT under any circumstance mix Java event
models unless completely sober. - It is possible to use both models in a single
project however, if a JDK 1.1 event listener is
registered to an object that uses JDK 1.02s
boolean event methods, the events will not be
delivered.
8Option 1 Listeners
- You are perhaps familiar with JDK 1.1 event
listeners interfaces. - Lets look at a common listener
- java.awt.event.MouseListener--
- public void mouseEntered(MouseEvent e)
- public void mouseExited (MouseEvent e)
- public void mouseClicked(MouseEvent e)
- public void mousePressed(MouseEvent e)
- public void mouseReleased(MouseEvent e)
9Listeners (contd)
- An event listener is useful where one has used up
the single inheritance to gain other needed
functionality (e.g., Applet, Frame). - Since a listener is introduced as an interface,
one must completely implement all the methods in
the interface class. - COST No-ops.
- BENEFITS Simple, singular class.
10- import java.awt.event. import java.awt.
import java.applet.Applet - public class test extends Applet implements
MouseListener - TextArea ta1 TextArea ta2
- public void init()
- super.init() //always!
- ta1 new TextArea(40,20) ta2 new
TextArea(40,20) - add(ta1) add(ta2) b.addMouseListener(thi
s) ta1.addMouseListener(this)
ta2.addMouseListener(this) -
- public void mouseEntered(MouseEvent e)
- TextArea t (TextArea) e.getSource()
- t.setBackground(Color.yellow)
-
- public void mouseExited (MouseEvent e)
-
- TextArea t (TextArea) e.getSource()
- t.setBackground(Color.white)
-
- public void mousePressed(MouseEvent e)
- public void mouseClicked(MouseEvent e)
11Option 2 Adapters
- Adapters are similar to listeners, exceptthat one
creates a unique class to handle the events. - The java.awt.event. package has a variety of
no-op classes that merely implement a
respective interface. - With an adapter, one merely overrides the desired
methods to dictate object behavior. - BENEFITS Highly OO, modular, abstract
- COST Not well understood
12Adapters What Are They Really?
- To test our understanding, lets guess at the
source code for a common adapter class - java.awt.event.MouseAdapter.
package java.awt.event / The adapter which
receives mouse events. The methods in this
class are empty this class is provided as a
convenience for easily creating listeners by
extending this class and overriding only the
methods of interest. _at_version 1.7 01/03/97
_at_author Carl Quinn / public abstract class
MouseAdapter implements MouseListener
public void mouseClicked(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
13- THIS IS MOST IMPORTANT PART OF ADAPTER CREATION.
- (really)
14- import java.awt. import java.applet. import
java.awt.event. - public class test2 extends Applet
- public void init()
- super.init()
- add(new TextArea(20,40))
- Component c this.getComponents()
- for (int i0 ilt c.length i)
- ci.addMouseListener(new
MyMouseAdapter(this)) -
-
- /
/ - public class MyMouseAdapter extends MouseAdapter
- test2 parent
- public MyMouseAdapter(test2 parent)
- this.parent parent
- public void mouseEntered(MouseEvent e)
- TextArea t (TextArea) e.getSource()
- t.setBackground(Color.yellow)
-
15Option 3 Semantic Events
- Event processing may also occur through the use
of semantic events. - Unlike listeners/adapters, these are high-level,
component-based events. - They essentially funnel a variety of low-level
events into a single method. - BENEFITS Few. (Easier in the short-term, but .
. . ) - COSTS Poor style. JDK 1.02-looking code.
16Semantic Events
17- import java.awt.
- import java.applet.
- import java.awt.event.
- public class test3 extends Applet implements
ActionListener - boolean toggle
- public void init()
- super.init()
- Button b new Button("Click Here")
- b.addActionListener(this)
- add(b)
-
- public void actionPerformed (ActionEvent e)
- ((Button)e.getSource()).setBackground(
- (!toggle) ? Color.yellow
Color.lightGray) - toggle !toggle
-
18Special Note Custom Components
- If you create your own lightweights, you may fire
semantic events by implementing the
processActionEvent() method. - More on this later. . . .
19Option 4 JDK 1.02
- JDK 1.02s event model is also know as the
inheritance based model. Since AWT components
inherit event methods, method handling occurs by
even passive, uninterested components. - Since all events delivered, regardless of use,
the model is painfully slow. - Also, the structure of event handling methods
encourages the development of linear C-like code.