Built-in Classes for Dialog Messages
Event handling is the process of handling events in a GUI application, and it can be implemented in different ways.
Three Programs With the Same Visible Result
Three small Java programs, named HelloWorldGUI1, HelloWorldGUI2, and HelloWorldGUI3, all do the same visible job: they show the message 'Hello World' in a window. Because the final result looks the same to the user, the differences between them are easy to miss. Those differences are the subject of this article. Each program reaches the same goal by a different route, and the routes teach different ideas about how a graphical program responds to events.
As you read, keep one question in mind: where does the code that deals with an event live, and how is it connected to the event? Answering that question for each of the three programs is the main skill this article builds.
What Happens When an Event Occurs
Event handling is the process of handling events in a GUI application, and it can be implemented in different ways. An event is something that happens while the program is running, such as the user acting on the window. A handler is the code that runs when an event occurs. Event handling is therefore a chain: something happens, the matching handler runs, and the user sees a result.
The key idea is that event handling is not a single technique. The source states that event handling can be implemented using different methods, including built-in classes and lambda expressions. The three Hello World programs are useful precisely because they expose these different methods side by side.
The diagram below traces the general pattern that all three programs share at a high level. Read it as a sequence of stages rather than a fixed recipe. The stage that changes from program to program is the one where the event handler is supplied, which is why the three versions are worth comparing directly.
Read the sequence from top to bottom. Step one is the user's action, which produces the event. Step two is the handler running, which is the code the program has associated with that event. Step three is the visible outcome. The question that separates the three programs is not whether these steps happen, since all three follow them, but how the handler gets attached to the event in the first place.
Showing a Message With JOptionPane
HelloWorldGUI1.java shows the message 'Hello World' in a window by using the built-in JOptionPane class. JOptionPane is a built-in class for displaying messages in a GUI application, and the source notes that it can be used to display messages in a window.
The main lesson of this first method is that the programmer does not have to construct the message window from scratch. The built-in class supplies the window, so the program's job is mostly to ask for the message to be displayed. This makes JOptionPane the simplest of the three approaches to read and to write.
What do you think happens?
In HelloWorldGUI1, who is responsible for constructing the message window?
Reveal answer
The built-in JOptionPane class supplies the window
The source explains that the programmer does not have to construct the message window from scratch. The built-in class supplies the window, and the program mainly asks for the message to be displayed.
Building the Window by Hand
HelloWorldGUI2.java also shows 'Hello World' in a window, but it builds the interface by hand rather than relying on the JOptionPane class. The program constructs the window and the parts it needs itself, instead of asking a built-in class to display a message.
When you build the interface by hand, you are responsible for more of the structure. This is more work than calling JOptionPane, but it gives the programmer direct control over how the window is assembled. The second program is therefore the bridge between the convenience of the first and the more compact style of the third.
Specifying the Handler With a Lambda
HelloWorldGUI3.java is similar to the second program, since it also builds the interface by hand. Its difference is that it uses a lambda expression to specify the event handler. The source notes that lambda expressions can be used to simplify event handling code.
The phrase specify the event handler is the important one. In the second program, the handler is set up in one way. In the third, the same job is described with a lambda expression, which is a compact way of writing the handler inline. Because the code is shorter, the connection between an event and its response can be easier to see on the page.
Choosing Between the Three Approaches
The three programs suggest a practical way to think about event handling. A built-in class such as JOptionPane is a good choice when you only need to display a message and want the class to supply the window. Building the interface by hand gives you more control over the structure. A lambda expression is a way to write the event handler more compactly within a hand-built interface.
| Program | How the window is built | How the handler is specified | Main advantage |
|---|---|---|---|
| HelloWorldGUI1 | Built-in JOptionPane class supplies the window | Program asks for the message to be displayed | Simplest to read and write |
| HelloWorldGUI2 | Program constructs the window and its parts | Handler is set up in the program | Direct control over how the window is assembled |
| HelloWorldGUI3 | Program constructs the window and its parts | Handler is specified with a lambda expression | Compact handler code, with the event-response link easier to see |
The three Hello World programs reach the same visible result by different routes.
Mistakes Learners Make Comparing the Three Programs
Common mistake Assuming the three programs behave differently for the user
Why it failsAll three programs show the same message, so the differences lie in how the window is built and how the handler is specified.
Do this insteadCompare the code structure, not the on-screen output, when studying these three examples.
Common mistake Thinking a lambda expression changes what the message says or looks like
Why it failsThe source describes the lambda as a way to write the event handler more compactly. It is about how the handler is specified.
Do this insteadAsk where the handler is defined and how it is connected to the event.
Common mistake Believing building by hand is always the better choice
Why it failsBuilding by hand is more work than calling
JOptionPane, although it gives more direct control over the window's structure.Do this insteadUse a built-in class like JOptionPane when you only need to display a message, and build by hand when you need more control.
Common mistake Forgetting that the event handler is the code that runs when the event occurs
Tracing the Three Programs Yourself
Take the three programs in this section and, for each one, write a short answer to two questions: Who supplies the window? Where is the code that runs when the event occurs? Then check whether your answer for HelloWorldGUI2 and HelloWorldGUI3 differs only in how the handler is specified.
Hints
- Start with HelloWorldGUI1. The source says the built-in class supplies the window.
- For HelloWorldGUI2 and HelloWorldGUI3, the source says both build the interface by hand.
- The source identifies the lambda expression as the difference between the second and third programs.
Key Takeaways
- Event handling is the process of handling events in a GUI application, and it can be implemented in different ways. A handler is the code that runs when an event occurs.
- HelloWorldGUI1 uses the built-in
JOptionPaneclass, which supplies the window so the program mainly asks for the message to be displayed. - HelloWorldGUI2 builds the interface by hand, giving the programmer more control over the window's structure but requiring more work.
- HelloWorldGUI3 is similar to HelloWorldGUI2 but uses a lambda expression to specify the event handler, which is a compact way of writing the handler inline.
- All three programs show the same message, so the differences lie in how the window is built and how the handler is specified.