| On each GUI platform a particular mouse button or combination of 
              mouse button and key strokes (such ast CTRL or ALT keys) brings 
              up a popup menu. A popup menu is a dialog window, usually displayed close to the 
              cursor where you clicked, that contains some items to initiate operations 
              related to what you clicked on (such as cut, copy, 
              paste in an editor.) To provide for platform portability, the AWT provides the isPopupTrigger() 
              method in the MouseEvent 
              class. It will indicate if the button/key combo for a popup menu 
              on the current OS has been activiated. For example, on Windows machines 
              this is the right mouse button. The PopupApplet program below illustrates the creation of a popup 
              menu to choose the background color for the component on which the 
              menu was requested. The program also illustrates the use of an annonymous 
              inner class, in this case a MouseAdapter, 
              for this kind of task.  
              
                 
                  | PopupApplet 
                        |   
                  | import 
                    javax.swing.*; import java.awt.*;
 import java.awt.event.*;
 
 /** 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);
 } // init
 
 } // 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. A PopupMenu
 * 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);
 
 // Create the popup menu
 makePopup ();
 
 // Add the MouseAdapter that opens 
                    the popup.
 canvas1.addMouseListener (fAdapter);
 canvas2.addMouseListener (fAdapter 
                    );
 } // ctor
 
 /** Create the popup menu and a MouseAdapter that 
                    opens it
 * when right button (or equivalent) 
                    is clicked.
 **/
 void makePopup () {
 fColorMenu = new JPopupMenu ("Color");
 fColorMenu.add (makeMenuItem ("Red") 
                    );
 fColorMenu.add (makeMenuItem ("Green") 
                    );
 fColorMenu.add (makeMenuItem ("Blue") 
                    );
 
 // Create a MouseAdapter that creates 
                    a Popup menu
 // when the right mouse or equivalent 
                    button clicked.
 fAdapter = new MouseAdapter () {
 
 // On some platforms, 
                    mouseReleased sets PopupTrigger.
 public void mouseReleased 
                    (MouseEvent e) {
 if (e.isPopupTrigger 
                    ()) {
 showPopupMenu 
                    (e);
 }
 }
 
 // And on other platforms, 
                    mousePressed sets PopupTrigger.
 public void mousePressed 
                    (MouseEvent e) {
 if (e.isPopupTrigger 
                    ()) {
 showPopupMenu (e);
 }
 }
 
 // Get the component over 
                    which the right button click
 // occurred and show the 
                    menu there.
 public void showPopupMenu 
                    (MouseEvent e) {
 fSelectedComponent 
                    = e.getComponent ();
 fColorMenu.show 
                    (fSelectedComponent, e.getX (), e.getY ());
 }
 
 }; // anonymous MouseAdapter subclass
 
 } // makePopup
 
 /** Change background color of selected components.**/
 public void actionPerformed (ActionEvent e) {
 String color = e.getActionCommand 
                    ();
 
 if (color.equals ("Red") )
 fSelectedComponent.setBackground 
                    (Color.red);
 else if(color.equals ("Green") )
 fSelectedComponent.setBackground 
                    (Color.green);
 else if (color.equals ("Blue"))
 fSelectedComponent.setBackground 
                    (Color.blue);
 else
 setBackground (Color.white);
 } // actionPerformed
 
 /** A utility method for making menu items. **/
 private JMenuItem makeMenuItem (String label) 
                    {
 JMenuItem item = new JMenuItem (label);
 item.addActionListener ( this );
 return item;
 } // makeMenuItem
 
 } // class PopupPanel
 |    The applet's interface consists of an instance of a subclass of 
               JPanel 
              called PopupPanel 
              to which two subpanels are added. An instance of a MouseAdapter 
              subclass, using the anonymous inner class technique, is created 
              and added to the MouseListener 
              list of both panels.  Mouse clicks therefore go to the adapter, which looks for the popup 
              menu request. If detected, then an instance of JPopupMenu 
              with the color menu items is displayed. (Systems differ as to whether 
              the popup signal is generated on a mouse button press or release 
              so both are tested here.)  When the user selects an item in the popup menu list, the actionPerformed() 
              method is invoked and the background color is set according to which 
              menu item was selected and over which panel.  Note that you don't have to respond 
              with a JPopupMenu 
              object in response to an affirmative isPopupTrigger() 
              response. You could always open a dialog 
              window, such as one with a JTextField 
              to enter a new value for something clicked on.    References & Web Resources Latest update: Dec. 6, 2004 |