Java Reference
In-Depth Information
Fig. 22.8
|
Test class for
PopupFrame
. (Part 2 of 2.)
Lines 44-69 register a
MouseListener
to handle the mouse events of the application
window. Methods
mousePressed
(lines 48-52) and
mouseReleased
(lines 55-59) check
for the pop-up trigger event. Each method calls private utility method
checkForTrigger-
Event
(lines 62-67) to determine whether the pop-up trigger event occurred. If it did,
MouseEvent
method
isPopupTrigger
returns
true
, and
JPopupMenu
method
show
dis-
plays the
JPopupMenu
. The first argument to method
show
specifies the
origin component
,
whose position helps determine where the
JPopupMenu
will appear on the screen. The last
two arguments are the
x
-
y
coordinates (measured from the origin component's upper-left
corner) at which the
JPopupMenu
is to appear.
Look-and-Feel Observation 22.9
Displaying a
JPopupMenu
for the pop-up trigger event of multiple GUI components re-
quires registering mouse-event handlers for each of those GUI components.
When the user selects a menu item from the pop-up menu, class
ItemHandler
's
method
actionPerformed
(lines 76-88) determines which
JRadioButtonMenuItem
the
user selected and sets the background color of the window's content pane.
A program that uses Java's AWT GUI components (package
java.awt
) takes on the look-
and-feel of the platform on which the program executes. A Java application running on a
Mac OS X looks like other Mac OS X applications, one running on Microsoft Windows
looks like other Windows applications, and one running on a Linux platform looks like
other applications on that Linux platform. This is sometimes desirable, because it allows
users of the application on each platform to use GUI components with which they're al-
ready familiar. However, it also introduces interesting portability issues.
Portability Tip 22.1
GUI components often look different on different platforms (fonts, font sizes, component
borders, etc.) and might require different amounts of space to display. This could change
their layout and alignments.
Portability Tip 22.2
GUI components on different platforms have might different default functionality—e.g.,
not all platforms allow a button with the focus to be “pressed” with the space bar.