Java Reference
In-Depth Information
The demonstration applet PopupApplet shown below illustrates the creation
of a popup menu (see Figure 12.2). Here the menu provides color options for the
background of the component on which the menu was requested. The program
also illustrates the use of an inner class for this kind of task.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Figure 12.2 The
PopupApplet program
demonstrates the
JPopupMenu component.
The user moves the
cursor over the top or
bottom panel and
executes the popup menu
procedure appropriate for
the platform, such as
clicking on the right
button of a two button
mouse on a MS Windows
system. The color of the
panel will be set
according to the menu
item selected.
/** Demonstration of popup menus. **/
public class PopupApplet extends JApplet
{
/** Create a simple interface with two panels. **/
public void init () {
Container content - pane = getContentPane ();
PopupPanel popup - panel = new PopupPanel ();
// Add the panel that will display the popup menu
content - pane.add (popup - panel);
}
} // class PopupApplet
/** Popup menu offers choices for colors of 2 subpanels. **/
class PopupPanel extends JPanel
implements ActionListener
{
MouseAdapter fAdapter;
JPopupMenu fColorMenu;
Component fSelectedComponent;
Component fParent;
/** Constructor creates an interface with two panels.
*APopupMenu will offer a choice of colors for the
*panels. **/
PopupPanel () {
setLayout (new GridLayout (2,1));
JPanel canvas1 = new JPanel ();
canvas1.setBackground (Color.RED);
add (canvas1);
JPanel canvas2 = new JPanel ();
canvas2.setBackground (Color.GREEN);
add (canvas2);
 
Search WWH ::




Custom Search