I agree. In addition, it makes coding the listener a little easier.
If you are only wanting to listen for a key being pressed, you do not
have to implement an empty keyReleased(Event e) method, you simply
implement the keyEvent(Event e) method and test to see if the event is a
keypress.
Again, this doesn't seem like a big deal in this example, but can mean a
big difference if there are a lot of different types of a kind of
event. MouseEvents for example. You might have MouseMoved, MouseClick,
MouseDoubleClick, MouseDrag, etc. You may only want to listen for a
click, why implement the others if you don't need them? AWT and Swing
have that kind of problem, for which Sun introduced the Adaptor
classes. MouseAdapter, for instance implements all of the 5 event
methods of MouseListener with empty methods.
I will not go into it any further as I'm sure everyone on this list are
aware of Swing's Adapter classes. I just wanted to point out how we may
be able to bypass what I see as a problem in Swing and, in so doing,
make our APIs easier to use.
On Thu, 2002-08-15 at 10:30, Rubio Jr. wrote:
> Umm, I like the second aproach. The event should be an object, it is
> something that can be represented by an abstract data type. It is not a
> behavior of an object.
>=20
>=20
>=20
> Rubio Jr.
>=20
>=20
> On Thu, 2002-08-15 at 16:55, Jeffrey Morgan wrote:
> > I would like to get everybody's opinion on something.
> > Is it better to define a method on a listener interface
> > for each type of event or is it better to define one
> > method and have the event class specify the type. For
> > example:
> >=20
> > Option 1: A method for each type of event
> >=20
> > public interface KeyListener {
> > public void keyPressed(KeyEvent event);
> > public void keyReleased(KeyEvent event);
> > };
> >=20
> >=20
> > Option 2: A single method with the KeyEvent
> > specifying the actual event.
> >=20
> > public interface KeyListener {
> > public void keyEvent(KeyEvent event);
> > };
> >=20
> > if (event.getId() =3D=3D KeyEvent.PRESSED)
> > ...
> >=20
> > Let me know what your thoughts.
> >=20
> > -Jeff
>=20
>=20
>=20
>=20
> -------------------------------------------------------
> This sf.net email is sponsored by: OSDN - Tired of that same old
> cell phone? Get a new here for FREE!
> https://www.inphonic.com/r.asp?r=3Dsourceforge1&refcode1=3Dvs3390
> _______________________________________________
> java-gnome-hackers mailing list
> jav...@li...
> https://lists.sourceforge.net/lists/listinfo/java-gnome-hackers
--=20
Philip A. Chapman
|