Java Reference
In-Depth Information
6. An event listener of type ActionListener can be registered with buttons, text
fields, text areas, radio buttons, combo boxes, check boxes, password fields, etc. The
actionPerformed method is executed whenever the data is modified.
7. A document listener (i.e., an object of type DocumentListener ) can be associated
with a document. The getDocument method can be called to extract a document
from a text field, text area, or a password field. A document listener can be used to
monitor for changes in the text.
8. A password field is very similar to a text field. However, there are two main differences.
First, asterisks appear to hide the text that is typed by the user. Second, for added
security, the getPassword method should be called to retrieve the password from the
password field. The method returns an array of characters.
9. There are three main differences between a dialog box (i.e., an object of type JDialog )
and a window (i.e., an object of type JFrame ). First, a dialog box does not have
minimize or restore buttons. Second, a dialog box must have a parent window. Thirdly,
a dialog box can be modal. This means that all the other windows of the application
will be inactive until the dialog box is closed.
12.16 Exercises
1. Create an application that calculates the fuel economy of a car. The application should
display a window. In the window, the user should type in the size of the gas tank and
how many miles he or she can travel with a full tank of gas. Both values should be
entered in text fields. When the user presses the Calculate button, the fuel economy
of the car should be displayed in a text field that is not editable.
2. Add a menu to the Notepad application. Add a menu item to count the number of
words in the text. The result should appear as a modal dialog box that contains the
result and a single OK button.
3. Add a menu item to the Notepad application that can wrap the text. Use the
setLineWrap method of the JTextArea class to achieve the wrapping.
4. Modify the freehand drawing application from Chapter 10. Add a panel that contains
three radio buttons: red, green, and blue. Add the panel to the south of the window.
Allow the user to choose the painting color using the radio buttons. Do not forget to
add the radio buttons to the same button group.
5. Modify the application from the previous question. This time, create a combo box
from which the user can choose the drawing color.
6. Create a simple sandwich-making program. Create a window that has multiple panels
with multiple components. For example, the user can select the toppings from a set
of check boxes (e.g., tomato, onion, pickles, mustard, ketchup, etc.). Using a combo
box here is a bad idea because several toppings can be selected. However, the main
ingredient of the sandwich can be selected from a combo box (e.g., chicken, tuna fish,
steak, etc.). Also, create a combo box for selecting the type of bread to use (e.g., white,
whole grain, wheat, etc.). You should also add a check box that can be used to specify
Search WWH ::

Custom Search