Section 1.6
The Modern User Interface

WHEN COMPUTERS WERE FIRST INTRODUCED, ordinary people -- including most programmers -- couldn't get near them. They were locked up in rooms with white-coated attendants who would take your programs and data, feed them to the computer, and return the computer's response some time later. When timesharing -- where the computer switches its attention rapidly from one person to another -- was invented in the 1960s, it became possible for several people to interact directly with the computer at the same time. On a timesharing system, users sit at "terminals" where they type commands to the computer, and the computer types back its response. Early personal computers worked in much the same way, except that there was only one person involved at a time. The type of interaction described here is called a command-line interface.

Today, of course, most people interact with computers in a completely different way. They use a Graphical User Interface, or GUI. The computer draws interface components on the screen. The components include things like windows, scroll bars, menus, buttons, and icons. Usually, a mouse is used to manipulate such components. Assuming that you are reading these notes on a computer, you are no doubt familiar with the basics of graphical user interfaces.

A lot of GUI interface components have become fairly standard. That is, they have similar appearance and behavior on many different computer platforms including Macintosh, Windows 3.1, Windows 95, and various UNIX window systems. Java programs, which are supposed to run on many different platforms without modification to the program, can use all the standard GUI components. They might vary in appearance from platform to platform, but their functionality should be identical on any computer on which the program runs.

Below is a very simple Java program -- actually an "applet," since it is running right here in the middle of a page -- that shows a few standard GUI interface components. The button, checkbox, text field, and pop-up menu are labeled. There are a few other components, besides. The labels themselves are components (even though they don't do very much). The lower half of the applet is a text area component, that can contain multiple lines of text. And in fact the whole applet is itself considered to be a "component" in Java terminology. Try clicking on the button and checkbox, or select an item from the pop-up menu. You can type in the text field, but you might have to click on it first to activate it:

Sorry, your browser doesn't do Java.
Here is what the applet looked like
on my computer:

Sorry again; no images either!

You'll find that messages are displayed in the text area as you experiment with the other components. What happens is that when you perform certain actions, such as clicking on a button, you generate an "event." A message is sent to the applet telling it that the event has occurred, and the applet responds as it is programmed to do, in this case just by displaying a message in the text area.

The use of the term "message" here is deliberate. Messages, as you saw in the previous section, are sent to objects. In fact, Java GUI components are implemented as objects. Java includes many predefined GUI classes that can be used in a Java program. Some of these classes are subclasses of others. Here is a diagram showing some of these classes and their relationships:

Don't worry about the details for now. But note that all the GUI classes are subclasses, directly or indirectly, of a class called Component. Two of the direct subclasses of Component themselves have subclasses. The classes TextArea and TextField, which have certain behaviors in common, are grouped together as subclasses of TextComponent. The class named Container refers to components that can contain other components. The Applet class is, indirectly, a subclass of Container since applets can contain components such as buttons and text fields.

Just from this brief discussion, perhaps you can see how GUI programming can make effective use of object-oriented design. In fact, GUI's, with their "visible objects," are probably a major factor contributing to the popularity of OOP.

Programming with GUI components and events is one of the most interesting aspects of Java. However, we will spend three chapters on the basics before returning to this topic in Chapter 5.

[ Next Section | Previous Section | Chapter Index | Main Index ]