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

52
|
Chapter 1, Basic JComponents
#12 Add Translucence to Menus
HACK
H A C K
#12
Add Translucence to Menus Hack #12
In this hack I will show you how to add true translucency to your menus with
only a slight modification to your program.
Computer interfaces are pretty sophisticated these days. Years ago, we con-
sidered ourselves lucky to simply have menu bars at all; now, we need
menus with sophisticated effects like animation, shadows, and translucency.
You’ve already seen how to achieve visual effects by overriding the
paint( )
method of a parent component and then rendering the children into a buffer
[Hack #9]. It would be nice to do the same thing here, but there’s just one small
problem. Overriding the
paint( ) method of the JMenu wouldn’t do any good
JMenu menu = new JMenu("File");
mb.add(menu);
menu.add(new JMenuItem("Open"));
menu.add(new JMenuItem("Save"));
menu.add(new JMenuItem("Close"));
menu.add(new JMenuItem("Exit"));
menu = new JMenu("Edit");
mb.add(menu);
menu.add(new JMenuItem("Cut"));
menu.add(new JMenuItem("Copy"));
menu.add(new JMenuItem("Paste"));
menu.add(new JMenuItem("Paste Special.."));
frame.getContentPane().setLayout(new BorderLayout( ));
frame.getContentPane( ).add("North",new JButton("Button"));
frame.getContentPane( ).add("Center",new JLabel("a label"));
frame.getContentPane( ).add("South",new JCheckBox("checkbox"));
frame.pack( );
frame.setSize(200,150);
frame.show( );
}
}
Figure 1-29. Menu with a drop shadow
Example 1-27. Putting the custom combo box into action (continued)

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