Everything2
Near Matches
Ignore Exact
Full Text
Everything2

Java event model

created by Space Butler

(thing) by Space Butler (5.2 y) (print)   ?   (I like it!) 1 C! Sat Dec 16 2000 at 0:19:37

(moved from event-driven language)

While Java 2 (JDK versions 1.2 and 1.3) is an object-oriented language, the event model was changed (after Java 1.0) to follow an event-driven model.

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 events (buttons, windows, 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 in Java 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!

printable version
chaos

How to exit FreeCell without losing the game event listener LU Decomposition Digital photography workflow
JMX Instrumentation Krugerrand December 16, 2000 occur
event-driven language
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
What you are reading:
Hurry up please it's time
Jerusalem artichoke
Butter
The Story of the Vivian Girls
models of fictional time travel physics
Objectivism
Josephine Baker in Berlin
O Fortuna
Balaclava
Homeschooling
George S. Patton
Aquatic Ape Theory
What is punk rock? What is the blues?
New Writeups
beatrice
You've been slowly taking me over for nearly a year, do you know that?(idea)
Berek
YouTube(thing)
shaogo
How to Pretend to Have a Job(idea)
hapax
Les Provinciales(review)
zoeb
The Scene(review)
aneurin
Telephone Numbers for drama purposes(idea)
Alnilamski
Cosmicopolis(fiction)
eien_meru
measure(idea)
Dreamvirus
pussy willow(thing)
czeano
Three "T"s(idea)
UncleM
Vantage Point 2: Fractal Thread Count(idea)
LostPsion
First Fiction: Night(fiction)
Lysander Peregrine
How to Pretend to Have a Job(idea)
antigravpussy
Our music, in place of breadcrumbs, is there to help us find our way back home(idea)
t3h_poker
She tastes like gingerbread, too.(fiction)
This page courtesy of The Everything Development Company