Concepts / Event Handling in GUI Programs

Event Handling in GUI Programs

MosaicDraw.java demonstrates menus and a color chooser dialog in a program where the user colors mosaic squares by clicking and dragging.

  • Programming

A Program That Colors Squares

Most graphical programs you use every day respond to actions: a click on a button, a choice from a menu, a window that pops up to ask a question. This article looks at a complete graphical program called MosaicDraw, introduced in Section 6.7 of the text. In MosaicDraw, the user colors the squares of a mosaic by clicking and dragging the mouse across the panel. Along the way, the program demonstrates two common parts of a graphical interface: menus and a color chooser dialog.

The goal is not to memorize every line of the program. The goal is to learn how to read a graphical program by asking one question about each part: what is this part responsible for? If you can answer that for the menus, the dialog, and the mouse handling, you can find where any feature lives in a program like this one.

Tracing One Click and Drag

The main activity of MosaicDraw is coloring squares by clicking and dragging. Follow a single action from start to finish. The user first presses the mouse on the mosaic panel. That panel is the area of the window where the squares appear, and it is the piece defined by MosaicCanvas.java. So the press happens on an area that MosaicCanvas defines.

Next, the user drags across several squares. Each movement of the mouse during the drag is an event, which is an action the user takes that the program must respond to. The responsibility for handling these events belongs to MosaicDrawController.java. The diagram below shows the order in which the user, the panel, and the controller are involved.

Who Builds What: The Two Files

MosaicDraw is built from two supporting files, and each has a different job. A helpful way to hold this split in mind is to ask two separate questions. First, what does the mosaic panel look like, and how is it defined? That question belongs to MosaicCanvas.java, which defines the mosaic panel itself. Second, who builds the window's pieces, and who responds when the user does something? That question belongs to MosaicDrawController.java.

The controller is the part that assembles the window. It creates the mosaic panel and it creates the menu bar. The MosaicCanvas file supplies the definition of the mosaic panel that the controller creates. The controller is also responsible for events, which are the actions the user takes, such as pressing and dragging the mouse or choosing from a menu. The diagram below shows these relationships.

Splitting the responsibilities this way makes the program easier to navigate. If you want to change how the mosaic panel is defined, you look in MosaicCanvas.java. If you want to change how the window is assembled or how a user action is answered, you look in MosaicDrawController.java.

Menus Versus Dialogs

Definition

A menu is a set of choices that the user can reach from a menu bar. A dialog is a separate window that appears to get input from the user. In MosaicDraw, the example uses a color chooser dialog, which lets the user pick the color used to fill the squares.

The two features are easy to confuse because both appear as part of the window's interface. The key difference is their role. A menu is a collection of choices organized under the menu bar. A dialog is a separate window whose purpose is to collect a piece of input, such as a color. The diagram compares the two side by side.

Predicting Who Builds the Menu Bar

What do you think happens?

Before reading on: in MosaicDraw, which file creates the menu bar, MosaicCanvas.java or MosaicDrawController.java?

  • MosaicCanvas.java

  • MosaicDrawController.java

Reveal answer
Answer:

MosaicDrawController.java

The source says the controller creates the panel and the menu bar, and it handles events. MosaicCanvas.java only defines the mosaic panel.

Following the Action a Second Time

Now retrace the same action, this time asking responsibility questions at each step. The point is to practice the habit of asking what each part is responsible for, rather than only remembering the sequence.

Worked example

Locating the Responsible File for a Drag

A user presses the mouse on the mosaic panel and drags across several squares. Which file defines the panel where the press happens, and which file is responsible for handling the drag events?

  1. 1
    Identify the surface

    The press happens on the mosaic panel, which is the area where the squares appear.

  2. 2
    Find the definition

    The source says the mosaic panel is defined by MosaicCanvas.java, so that is the file that defines the surface being pressed.

  3. 3
    Identify the events

    Each movement during the drag is an event that the program must respond to.

  4. 4
    Find the handler

    Event handling is the responsibility of MosaicDrawController.java, so the drag events are handled there.

Result

MosaicCanvas.java defines the panel where the press occurs, and MosaicDrawController.java handles the press and drag events.

Mistakes Beginners Make When Reading GUI Code

  • Common mistake

    Assuming the panel handles its own events

    What it looks like

    Looking only in MosaicCanvas.java for what happens when the user drags the mouse.

    Why it fails

    The source places event handling in MosaicDrawController.java, while MosaicCanvas.java defines the panel.

    Do this instead

    Ask who is responsible for responding to user actions, and look in the controller first.

  • Common mistake

    Treating a menu and a dialog as the same thing

    What it looks like

    Calling the color chooser a menu because it is part of the same window.

    Why it fails

    A menu is a set of choices reached from a menu bar, while a dialog is a separate window used to get input such as a color.

    Do this instead

    Classify a feature by its role: organized choices are a menu, and a separate window collecting input is a dialog.

  • Common mistake

    Forgetting that each mouse movement during a drag is a separate event

Practice Tracing and Classifying

EASY

Using only the MosaicDraw description, answer two questions. First, a user chooses a fill color from the color chooser. Is that feature a menu or a dialog, and why? Second, name the file that defines the mosaic panel and the file that creates the menu bar.

Hints
  • Ask whether the feature is a set of choices reached from the menu bar or a separate window that gets input.
  • Remember that MosaicCanvas.java defines the panel, while MosaicDrawController.java creates the panel and the menu bar.

Key Takeaways

  • MosaicDraw demonstrates menus and a color chooser dialog in a program where the user colors mosaic squares by clicking and dragging.
  • MosaicCanvas.java defines the mosaic panel, while MosaicDrawController.java creates the panel and menu bar and handles the user's events.
  • A click-and-drag action starts on the mosaic panel, and each movement during the drag is an event handled by the controller.
  • A menu is a set of choices reached from a menu bar, while a dialog is a separate window used to get input such as a color.