Java Reference
In-Depth Information
This is a convenient shortcut that you will find in many programs, but it does
muddle the responsibilities between the frame class and the program. Therefore,
we do not use this approach in this topic.
10.10 Processing Text Input
A graphical application can receive text input by calling the showInputDialog
method of the JOptionPane class, but popping up a separate dialog box for each
input is not a natural user interface. Most graphical programs collect text input
through text fields (see Figure 12 ). In this section, you will learn how to add text
fields to a graphical application, and how to read what the user types into them.
The JTextField class provides a text field. When you construct a text field, you
need to supply the widthȌthe approximate number of characters that you expect the
user to type.
final int FIELD_WIDTH = 10;
final JTextField rateField = new
JTextField(FIELD_WIDTH);
Users can type additional characters, but then a part of the contents of the field
becomes invisible.
Use JTextField components to provide space for user input. Place a JLabel
next to each text field.
You will want to label each text field so that the user knows what to type into it.
Construct a JLabel object for each label:
JLabel rateLabel = new JLabel(ÐInterest Rate: Ñ);
You want to give the user an opportunity to enter all information into the text fields
before processing it. Therefore, you should supply a button that the user can press to
indicate that the input is ready for processing.
Search WWH ::




Custom Search