
Add Translucence to Menus #12
Chapter 1, Basic JComponents
|
55
HACK
The custom pop-up menu UI used here is similar to the CustomMenuItemUI
(from Example 1-28). It has a static create UI menu and no constructor. The
pop-up menu is already stored as a protected member of the
BasicPopupMenuUI
parent class, so I can access it easily. The
installUI( )
method is called right after the
JPopupMenu
is created, so this is the best place
to put a call to
setOpaque(false)
. For most L&Fs, this will make the compo-
nent transparent.
That takes care of the pop-up menu, but what about the parent
JPanel? The
JPanel
is created and initialized deep within the
javax.swing.PopupFactory
class, so it’s pretty well out of reach. This is one place where having access
to the JRE source code is invaluable. Without that, this entire hack would
have been impossible to figure out. Fortunately, we have access to the fin-
ished
JPopupMenu from within the getPopup method. I overrode that to call
the superclass and then grab the newly minted parent of the pop-up menu
and cast it to a
JPanel. Now, I can finally set it to be transparent, too.
Test It Out
With your two custom UI classes in place, test them out with Example 1-30,
which shows a frame containing two sets of menus and a few components.
Before creating any components, the program installed the custom UI
classes with two calls to
UIManager.put( ).
Any time you want to override part of