Object-Oriented Programming in Java
User interactions with GUI components generate events.
Looking Versus Interacting
A graphical user interface, or GUI, is the part of a program that shows windows, buttons, and other visual pieces on the screen. It is tempting to think of a window as something that is simply displayed and then waits passively. The source material makes an important point about this: a GUI does not respond just because a user looks at it. Something has to happen through interaction, such as a push button being clicked or a keyboard key being pressed. Those interactions are what generate events, and the program responds to the events it receives.
What do you think happens?
A window with a push button is open on the screen. The user reads the window carefully but does not click, type, or press anything. What does the program do in response?
Reveal answer
Nothing happens, because no interaction has occurred.
The source states that a GUI does not respond because a user merely looks at it. Events come from interactions such as clicking a push button or pressing a key, so without an interaction there is no event for the program to respond to.
Tracing a Single Click
Follow one interaction from start to finish. A user clicks a push button on the window. The click is an interaction with a GUI component, and that interaction generates an event. The program does not simply see the click happen by itself. It receives a message about the event, which informs it that an event has occurred. Then the program responds according to the instructions it contains, and those instructions are carried out by an event handler.
Read the diagram from top to bottom. The user acts on the button, the button's interaction generates the event, the program is informed through a message, and the handler supplies the response. Each arrow is a step that happens in order, so the handler does not run until the program has received word that the event occurred.
What Triggers an Event
Events are generated by user interactions with GUI components. The source names two examples. A push button may be clicked, and a keyboard key may be pressed. Both are actions a user takes, and both produce events. The source does not present these two as a complete list, so think of them as the standard illustrations of the principle: an interaction with a component is what creates an event.
An event is the result of a user interaction with a GUI component, such as clicking a push button or pressing a keyboard key.
How the Program Learns an Event Occurred
An event that happens on the screen is not automatically understood by the program's code. The program is informed that an event has occurred through a message about the event. The program receives that message and then decides, according to the instructions it contains, how to respond. This two-part arrangement is the key idea: the interaction creates the event, and a message carries the news of that event to the program.
Notice what the message does and does not do. It tells the program that something happened. It does not itself contain the response. The response lives in the instructions the program holds, which is where event handlers come in.
Event Handlers as Response Instructions
An event handler provides the instructions for responding to an event. When the program is informed that an event has occurred, the handler is the part that says what to do next. The source also notes that a GUI program typically consists largely of event handlers. In other words, a large share of a GUI program's code is made up of these response instructions, one set for each kind of event the program needs to answer.
A Submit Button Gets Clicked
A window contains a Submit button and a Cancel button. The user clicks Submit. Trace how the program responds, using only the event ideas from this article.
- 1Interaction
The user clicks the Submit push button. This is a user interaction with a GUI component.
- 2Event generated
The interaction generates an event. The event is associated with the Submit button, the component the user acted on.
- 3Program informed
The program receives a message informing it that an event has occurred.
- 4Handler responds
The program follows the instructions for this event, which are provided by the event handler for the Submit button. The Cancel button was not clicked, so its handler is not involved in this trace.
Only the Submit event's handler responds. The window stays idle until a user interaction produces another event.
Comparing Interactions and Their Results
Now trace a few different situations side by side. The same chain of steps applies whenever an interaction happens: the interaction generates an event, the program receives a message, and the handler responds. When no interaction happens, the chain never starts.
| Situation | Event generated? | What the program does next |
|---|---|---|
| User clicks a push button | Yes | Receives a message about the event and runs the handler's instructions |
| User presses a keyboard key | Yes | Receives a message about the event and runs the handler's instructions |
| User only looks at the window | No | Nothing happens, because no interaction has occurred |
How different situations lead to different program behavior
Components Are Objects, Organized by Class
The GUI components that appear on screen are not unrelated bits of decoration. Java GUI components are objects. Java provides many predefined classes, and each class represents a different kind of GUI component. A specific component on the screen is an object created from one of these classes. That is why the event ideas above, such as a push button being clicked, connect to objects in the program.
The diagram shows the two relationships the source describes. First, component classes relate to one another: some of them are subclasses of other classes, so they sit in a class structure. Second, an individual component you can see and interact with is an object that belongs to one of those classes. The source does not list the specific component classes, so the diagram stays general on purpose.
Mistakes Beginners Make with Events
Common mistake Expecting a window to respond just because it is on screen
What it looks likeA learner assumes that opening a window will make the program run its button code without anyone touching the button.
Why it failsThe source states that a GUI does not respond because a user merely looks at it. Something must happen through interaction.
Do this insteadConnect every response to a specific interaction, such as a click or key press, that generates an event.
Common mistake Thinking the message contains the response
What it looks likeA learner expects the notification about the click to contain the code that should run.
Why it failsThe message informs the program that an event has occurred. The instructions for responding are held by the event handler.
Do this insteadKeep the two roles separate: the message reports the event, and the handler supplies the response.
Common mistake Treating GUI components as simple screen decoration
Why it failsThe source states that Java GUI components are objects, not unrelated pieces of screen decoration, and that they belong to predefined classes.
Do this insteadThink of each button or other component as an object created from a class that may be a subclass of another class.
Trace an Interaction Yourself
A GUI window contains a push button. The user first clicks the button and then presses a keyboard key. Trace what the source says happens for each interaction. How many events are generated, and what does the program receive for each one?
Hints
- Ask first whether each action is an interaction with a GUI component.
- The source says each interaction generates an event, and the program is informed of each event by a message.
- Remember that a message reports the event, while the handler supplies the response.
Key Takeaways
- User interactions with GUI components, such as clicking a push button or pressing a key, generate events. A GUI does not respond merely because a user looks at it.
- A message informs the program that an event has occurred, so the program learns about the event through this message.
- Event handlers provide the instructions for responding to events, and a GUI program typically consists largely of event handlers.
- Java GUI components are objects created from predefined classes, and some of these classes are subclasses of other classes, forming a class structure.