(moved from event-driven language)
While Java 2
) is an object-oriented
language, the event model
was changed (after Java 1.0
) to follow an event-driven
Basically how it works is that within the program code
, you create an object
that can receive an event -- almost any GUI
-related object can receive event
, text fields
, etc.). Once the object is created, you create what's called a listener
object -- an object containing certain methods
that are called at the appropriate time. See event listener
for more information on event listeners in Java 2, once I put a writeup there.
Once the listener object is created, you associate
it with the object that will receive the events. This is done by calling the appropriate method on the object and giving it the listener object as an explicit argument
. For example:
JButton button = new JButton("Foo"); // creates a button with the text "Foo"
SomeListenerClass listener = new SomeListenerClass(); // Create the listener object
button.addActionListener(listener); // Adds the listener to the button
So, the event listener has been added. What now?
Well, as detailed in event listener
, there are different types
of listeners, depending on exactly what you want to listen for. Above we used the listener as an ActionListener
, which is sort of a basic listener class that'll get called when any of a number of different events occur (clicking, hitting 'enter' while the control
(in this case button) is selected, etc.). When one of the listened-for events occurs, the appropriate method on the listener object is called
, and thus the code in it is executed
. (Again, this is gone over in more detail in event listener
, or will be when I write it.)
I gather the event model
1 and 1.1 was different
; however, I'm not familiar with the differences between Java 1 and 2, so I'll leave that for someone more qualified
to node about.
Update 2000/12/15: yerricde
just pointed out to me that the Java 1.1 AWT
(Abstract Windowing Toolkit
-- the most basic GUI packages for Java, and by extension the rest of the GUI packages, like Swing
) also uses an event-based model, but Java 1.0
does not. Thanks, yerricde!