Java Reference
In-Depth Information
Creating a Multiple Window User Interface
J ava 2
v 5 . 0
J ava 2
As shown in Figure 10-1 on page 605, the StockListDemo application has two
windows that comprise the user interface. One window, created by the Logon-
Frame class, displays the logon window that allows a user to log on to the applica-
tion with an existing user name and password. Because it is a test application, this
same logon window also is used to add new users or to allow a user to change his
or her password. In a completed or production application, rather than a test
application, the functions of adding a new user or changing a password likely
would be completed in a different window, with access limited to valid users or
administrators of the system. A second window, created by the StockFrame class,
is used to simulate a working stock list application. This window displays data for
the current user and provides a means for the user to log off.
Because these two windows are not displayed simultaneously, the current
window must close when control transfers to the other window. To provide this
functionality, the LogonFrame object creates and then displays a StockFrame
object, and, at that time, it can close, or hide itself from the user. When the
StockFrame object closes, however, the program needs a way to notify the
LogonFrame so it can redisplay. A callback mechanism provides this functionality.
In Java, a callback mechanism is a way for one object to provide a generic refer-
ence to itself, so that a second object can call back to a known method of the first
object. The method to which the second object calls back is referred to as the
callback method . To guarantee that the callback method is present in the calling
object, the calling object must implement an interface that requires that method.
Interestingly, the called object does not need to know anything about the
calling object, as long as the calling object implements the required interface. In
fact, any object of any type can be called back, as long as it implements the
required interface. Recall from Chapter 1, the object-oriented concept of poly-
morphism means that instructions can be given to an object in a generalized
rather than object-specific command. In this case, the callback is polymorphic,
because the exact type of the called object is not known at compile time.
JAVA UPDATE
v 5 . 0
Emumerated Types
(Enums)
Instead of using of a
series of unrelated
named int constants to
represent a set of val-
ues (e.g., Quarters of
FALL = 0, WINTER = 1,
SPRING = 2, SUMMER
= 3), Java now supports
true, more flexible,
enumerated types with
the reserved word,
enum. Enums are
implicitly public static
final (may declare
explicitly as private)
and provide constants
in a typesafe manner,
so usage can be limited
to valid values and
actions. (For example,
Quarter = SPRING +
WINTER does not make
sense, nor does Quarter
= 7, but this is valid
when using int con-
stants.) Enums created
in your code are a sub-
class of java.lang.Enum
and so have the ability
to inherit (as with the
toString(), valueOf(),
and values() methods)
and to contain addi-
tional data and meth-
ods. Enums can be
used in switch() state-
ments, compared with
equals or ==, and
sorted. They cannot
be defined locally.
The code to the right
shows an enum in the
simplest form as a list
of constants, and as a
more complex class,
with added data and
methods.
// simple enum
private enum Quarter { FALL, WINTER, SPRING, SUMMER } // values of 0, 1, 2, and 3, respectively
Quarter session = Quarter.WINTER; //assigns to variable, session
System .out.println ( "Session = "+session ) ; //prints WINTER using toString()
session = Quarter.value0f ( "SPRING" ) ; //sets session to Quarter.SPRING
System .out.println ( "Session = "+session ) ; //prints SPRING
public enum Grade // enum class definition
{
A ( "Superior" ), B ( "Good" ) , C ( "Average" ) , D ( "Poor" ) , P ( "Passing" ) , F ( "Failed" ) , I ( "Incomplete"
private final String desc;
Grade ( String desc )
{
this .desc = desc;
}
public String desc( )
{
return desc.toString () ;
}
}
//values() returns an array of Grade, each element has one of the enumerated type's -lues
for ( Grade g : Grade.values ())
System .out.print ( g + ": \t" + g.desc ()) ;
 
Search WWH ::




Custom Search