Graphical user interfaces (GUI) offers a profoundly different programming
environment than the step-by-step, linear world of procedural programs.
The GUI presents many features all appearing to operate independently
and in parallel, even though usually run with a single processor.
GUIs match well with the modularity of object oriented languages
since the many different parts of a GUI can be created as class
objects.
In the procedural world, the user simply starts a program and waits
for it to churn through its algorithm and eventually reach the end
and stop. In the GUI enviroment, the program instead waits for the
user to select some task and then it carries out that selected action.
Afterwards, the program returns to a waiting state. (Meanwhile,
the processor, which could be physical or a Virtual Machine, will
switch frequently to other tasks.)
This wait state could involve a loop that checks a flag each pass
or a sleep state that is interrupted by the OS, or in the case of
Java, by the JVM. When the user moves a mouse, clicks one of its
buttons, hits a key, or initiates some other signal, the operating
system detects this and passes the information to the JVM, which
creates an event object to carry information about the event
to the program.
In version 1.0 of Java, when a mouse clicked over a button, the
event first went to the event handling code of the lowest level
container and then up through all the sub-components until one of
them dealt with the event. Only subclasses of the Component
class could handle events.
In version 1.1 came a more flexible and less wasteful event handling
system in which events are sent just to those components that register
to receive such events.
The event registering scheme also allows for custom components,
such as those in Swing. A customized visual component can ask to
have relevant events sent to it just like any other component. Also,
a class this not a Component
subclass can implement the handling (or listener) interface
for a particular type of event, such as a button press.
|