
Add a Third Dimension to Swing #63
Chapter 8, Rendering
|
319
HACK
As you can see, you end up with a black background in the Canva3D. Because
the only way to get rid of a component’s background is to call
setOpaque(false)
—which is defined by
JComponent
and thus isn’t available
to AWT components—you are stuck with this ugly background. Indeed, as
a lightweight component, the canvas cannot be made transparent. Things
get even worse when you try to add a menu bar to the application because of
the order in which components are painted: lightweight first, heavyweight
next. Figure 8-10 shows an example of what happens when a pop-up menu
is drawn by Swing. Because it is a lightweight component, it is drawn before
Canvas3D
, when it should be drawn after the canvas.
Thankfully, this new problem (it’s all AWT’s fault!) was so annoying that
the Swing team decided to add a workaround for it. You can simply force all
pop-up menus of your applications to be created as heavyweight compo-
nents instead of lightweight components. A single line of code is enough to
fix the problem:
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
If you invoke this method before you create the first JMenu or JPopupMenu,
you ensure your menus will be drawn on top of heavyweight components.
So, this takes care of one issue, but you still need to deal with the black
background problem.
Faking Transparency
Because you cannot change the