Page 688 - Introduction to Programming with Java: A Problem Solving Approach
P. 688

                654 Chapter 16 GUI Programming Basics
public void addActionListener(ActionListener listener)
Adds a listener to the text box.
Text boxes are editable by default, which means that users can type inside them. If you want to pre- vent users from editing a text box, call setEditable with an argument value of false. Calling setEditable(false) prevents users from updating a text box, but it does not prevent programmers from updating a text box. Programmers can call the setText method regardless of whether the text box is editable or non-editable.
Components are visible by default, but there are some instances where you might want to call setVisible(false) and make a component disappear. After you calculate a result, you might want just the result to appear without the clutter of other components. When a component is made to disappear, its space is automatically reclaimed by the window so other components can use it.
When a JTextField component calls addActionListener, the JVM attaches a listener object to the text box, and that enables the program to respond to the user pressing Enter within the text box. We’ll cover listeners in more detail soon enough, but first we’re going to step through an example program that puts into practice what you’ve learned so far. . . .
16.8 Greeting Program
In Figures 16.5a and 16.5b, we present a Greeting program that displays a personalized greeting. It reads the user’s name from a text box (a JTextField component) and displays the entered name in a label (a
 JLabel component).
Most of the code in the Greeting program should look familiar since it closely parallels the code
Apago PDF Enhancer
in the SimpleWindow program. For example, note the short main method with the anonymous ob- ject instantiation. Note the constructor that contains calls to setTitle, setSize, setLayout, setDefaultCloseOperation, and setVisible. Finally, note the createContents helper method that creates the components and adds them to the window. Now let’s focus on what’s new about the Greeting program—a text box and an event handler.
The Greeting program uses a text box, nameBox, to store the user’s name. Note how the createContents method instantiates nameBox with a width of 15. Note how the createContents method calls the add method to add nameBox to the window. That code is straightforward. But something that’s not so straightforward is nameBox’s declaration. It’s declared as an instance variable at the top of the class. Why an instance variable instead of a createContents local variable? Aren’t local variables preferred? Yes, but in this case, we need to access nameBox not only in createContents, but also in the actionPerformed event handler (which we’ll get to next). It’s possible to use a local variable within createContents and still access it from the event handler, but that’s a bit of a pain.1 For now, we’ll keep things simple and declare nameBox as an instance variable. The same rationale applies to the greeting label. We need to access it in createContents and also in the actionPerformed event handler, so we make it an instance variable.
The Greeting program’s actionPerformed event handler specifies what happens when the user presses Enter within the text box. Note that the actionPerformed method is inside our Listener class. We cover listeners and event-handler mechanics in the next section.
1 If you declare a variable locally within createContents, you can retrieve it from an event handler by calling getSource. The getSource method is covered in Section 16.14.
 



















































































   686   687   688   689   690