J2SE up to version 1.5 doesn't allow you to easily create translucent menus as in the screenshot below.
At first you might think that calling
menu.getPopupMenu().setOpaque(false) would be enough. Wrong. When you click onto a menu, the popup does not appear directly on screen. It is first put in a container. This container is a JPanel for lightweight popup menus for instance. Therefore, you need to get the parent of the menu's popup and set its opaque property to false. Wow, we end up with:
JMenu menu = new JMenu("File"); JMenuItem item = new JMenuItem("Open"); item.setOpaque(false); JPopupMenu popup = menu.getPopupMenu(); popup.setOpaque(false); // the following line must be executed only when // the poup menu is shown popup.getParent().setOpaque(false);
To be truly honest this is not a big deal for most applications. Unfortunately this is a show stopper for Synth. Check out this excerpt of a Synth theme file:
<style id="popupMenuStyle"> <opaque value="false"/> <insets top="2" bottom="4" right="4" left="2"/> <state> <imagePainter method="popupMenuBackground" path="images/popupMenu.png" sourceInsets="5 5 3 3"/> </state> </style> <bind style="popupMenuStyle" type="REGION" key="PopupMenu"/>
The picture used in this image painter is supposed to drop a shadow below the popup menu. Unfortunately, and even if opaque is set to false, this cannot work. The opaque property is effectively set to the popup menu but not to its parent.
Rejoice, this is fixed in Mustang. Well, at least for lightweight popup menus :)
P.S: You can download the source code of the example shown in the screenshot.