O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Add Translucence to Menus #12
Chapter 1, Basic JComponents
|
57
HACK
problem, you’ll need to develop a full repaint manager [Hack #53] that will force
Swing to always repaint the entire component tree, instead of just the menu
items. It’s a bit slower, but worth it if you really want this effect:
UIManager.put("MenuItemUI","CustomMenuItemUI");
RepaintManager.setCurrentManager(new FullRepaintManager( ));
One more bug is that the menu must fit within the frame. There are two
kinds of menus in Swing: heavyweight and lightweight. Lightweight menus
are normal Swing components. Heavyweight menus, on the other hand, are
drawn in their own top-level window. This means that there are two win-
dows being drawn: one for the real frame and one for the menu. If you use
heavyweight menus, the effect will stop completely because the windows
themselves can’t be transparent. Normally, Swing will use lightweight
menus, but if the menu has to be drawn outside of the frame—which can
happen if you have a small window or a really large menu—then it will
switch to heavyweight menus automatically and nothing can switch it back
until the application restarts. This means you should always make sure your
menus fit inside of your windows.
Future Ideas
This hack shows just one example of how you can completely change a
component’s behavior by customizing its Look and Feel class. Java2D gives
you the power to create a wide variety of graphical hacks. As an extension of
this technique, you could try blurring the components underneath the menu
or create a properly smoothed drop shadow.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required