26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 Graphical User Interface Components: Part 1 659<br />

Fig. Fig. 12.7 12.7 Demonstrating JTextFields and JPasswordFields (part 4 of 4).<br />

Lines 12–13 declare three references for JTextFields (textField1,<br />

textField2 and textField3) and a JPasswordField (passwordField).<br />

Each of these is instantiated in the construc<strong>to</strong>r (line 16–50). Line 24 defines JTextField<br />

textField1 with 10 columns of text. The width of the text field will be the width in<br />

pixels of the average character in the text field’s current font multiplied by 10. Line 25 adds<br />

textField1 <strong>to</strong> the content pane.<br />

Line 28 defines textField2 with the initial text "Enter text here" <strong>to</strong> display<br />

in the text field. The width of the text field is determined by the text. Line 29 adds<br />

textField2 <strong>to</strong> the content pane.<br />

Line 33 defines textField3 and call the JTextField construc<strong>to</strong>r with two arguments—the<br />

default text "Uneditable text field" <strong>to</strong> display in the text field and the<br />

number of columns (20). The width of the text field is determined by the number of columns<br />

specified. Line 34 uses method setEditable (inherited in<strong>to</strong> JTextField from<br />

class JTextComponent) <strong>to</strong> indicate that the user cannot modify the text in the text field.<br />

Line 35 adds textField3 <strong>to</strong> the content pane.<br />

Line 38 defines JPasswordField passwordField with the text "Hidden<br />

text" <strong>to</strong> display in the text field. The width of the text field is determined by the text.<br />

Notice that the text is displayed as a string of asterisks when the program executes. Line 39<br />

adds passwordField <strong>to</strong> the content pane.<br />

For the event-handling in this example, we defined inner class TextFieldHandler<br />

(lines 62–92), which implements interface ActionListener (class TextFieldHandler<br />

is discussed shortly). Thus, every instance of class TextFieldHandler is an<br />

ActionListener. Line 42 defines an instance of class TextFieldHandler and<br />

assigns it <strong>to</strong> reference handler. This one instance will be used as the event-listener object<br />

for the JTextFields and the JPasswordField in this example.<br />

Lines 43–46 are the event registration statements that specify the event listener object for<br />

each of the three JTextFields and for the JPasswordField. After these statements<br />

execute, the object <strong>to</strong> which handler refers is listening for events (i.e., it will be notified<br />

when an event occurs) on these four objects. The program calls JTextField method<br />

addActionListener <strong>to</strong> register the event for each component. Method addAction-<br />

Listener receives as its argument an ActionListener object. Thus, any object of a<br />

class that implements interface ActionListener (i.e., any object that is an Action-<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!