Java Reference
In-Depth Information
TitledBorder object is a Border object with text that, by default, displays in a
line surrounding the component for which it is used. In line 67, a TitledBorder
object using the string, “Enter user info,” for the title text, is created and then
passed to the setBorder() method, which sets a Border object for the JPanel.
In line 120, the method getDefaultToolkit() gets the default implementation
of the AWT(Toolkit) for the current platform. The getScreenSize() method of
the Toolkit object gets the size of the screen in pixels and returns a Dimension
object, which encapsulates the width and height of the screen. This allows the
width and height of the screen to be used to center the current component.
Lines 160 through 166 create a doStockActivity() method that creates a new
StockFrame object and makes it visible after hiding the LogonFrame. Lines 168
through 173 include code for the callback method, activate().
Lines 175 through 306 include code for the actionPerformed() method, which
is required to implement the ActionListener interface. The actionPerformed()
method catches the various password exceptions to allow for better error handling
than catching only general exceptions. Line 201 checks if the Add new user button
was clicked; if so, line 205 creates the new user object with the given user name
and password that automatically expires after four uses. Line 214 tests if the Log on
button was clicked. If so, line 216 verifies that the user is in the user list. For an
existing user, line 218 validates the password entered for the user. For a valid pass-
word, line 220 checks if the password is expiring, and if so, displays a message indi-
cating the number of remaining uses. For a valid user with a valid password, line
223 calls the doStockActivity() method which was created earlier. Line 229 tests if
the Change user pswd button was clicked. If so, line 231 verifies that the user is in
the user list. For an existing user, line 233 validates the password entered for the
user. For a user with a valid password, the user is presented with a screen to enter a
new password. Line 240 changes the password for the current user and then line
243 calls the doStockActivity() method. Note that beginning in line 256, the excep-
tions caught are in order from more specific to more general, up to and including
a general Exception.
J ava 2
v 5 . 0
JAVA UPDATE J ava 2
v 5 . 0
Enhanced for() Loop
When looping through
all items in an array or a
Collection, such as an
ArrayList, instead of
incrementing an index
and comparing it to the
size of the array or
Collection to avoid
exceeding the bounds,
Java 5.0 provides for an
enhanced for() loop.
Using the enhanced
for() loop simplifies
code, especially as com-
pared to iterating
through a Collection
with an Iterator object.
Because you do not use
an index value and the
only test for continuing
the loop is whether you
have accessed all ele-
ments, the only way to
prematurely exit the
loop is by using the
break statement, which
is unstructured. In the
right circumstances, the
enhanced for() loop can
be very useful, but use
it with care and do not
expect to use it in all
loop situations. To help
understand the new
syntax, read it as "for
each Type variableName
in array/Collection", as
in "for each int num in
numArray" or "for each
User user in userList".
for ( int num : intArray ) // for each int "num" in array of int "intArray"
total += num;
// auto-unboxing Integer j to int, add to total
for ( Integer j : list ) // for each Integer "j" in ArrayList "list"
sum += j; // auto-unboxing Integer j to int, add to sum
The following code
shows use of the
enhanced for() loop
with an array of int
(intArray) and with an
ArrayList of Integers
(list), summing the
contents of each into
an int variable.
FIGURE 10-49
Search WWH ::




Custom Search