Many of today's computer applications provide so-called context-sensitive pop-up menus. In Swing, such menus are created with class JPopupMenu (a subclass of JComponent). These menus provide options that are specific to the component for which the popup trigger event was generated. On most systems, the pop-up trigger event occurs when the user presses and releases the right mouse button.
Look-and-Feel Observation 22.9
The pop-up trigger event is platform specific. On most platforms that use a mouse with multiple buttons, the pop-up trigger event occurs when the user clicks the right mouse button on a component that supports a pop-up menu. |
The application in Figure 22.7 and Fig. 22.8 creates a JPopupMenu that allows the user to select one of three colors and change the background color of the window. When the user clicks the right mouse button on the PopupTest window's background, a JPopupMenu containing colors appears. If the user clicks a JRadioButtonMenuItem for a color, ItemHandler method actionPerformed changes the background color of the window's content pane.
Figure 22.7. JPopupMenu for selecting colors.
(This item is displayed on pages 1020 - 1021 in the print version)
1 // Fig. 22.7: PopupFrame.java 2 // Demonstrating JPopupMenus. 3 import java.awt.Color; 4 import java.awt.event.MouseAdapter; 5 import java.awt.event.MouseEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.ActionEvent; 8 import javax.swing.JFrame; 9 import javax.swing.JRadioButtonMenuItem; 10 import javax.swing.JPopupMenu; 11 import javax.swing.ButtonGroup; 12 13 public class PopupFrame extends JFrame 14 { 15 private JRadioButtonMenuItem items[]; // holds items for colors 16 private final Color colorValues[] = 17 { Color.BLUE, Color.YELLOW, Color.RED }; // colors to be used 18 private JPopupMenu popupMenu; // allows user to select color 19 20 // no-argument constructor sets up GUI 21 public PopupFrame() 22 { 23 super( "Using JPopupMenus" ); 24 25 ItemHandler handler = new ItemHandler(); // handler for menu items 26 String colors[] = { "Blue", "Yellow", "Red" }; // array of colors 27 28 ButtonGroup colorGroup = new ButtonGroup(); // manages color items 29 popupMenu = new JPopupMenu(); // create pop-up menu 30 items = new JRadioButtonMenuItem[ 3 ]; // items for selecting color 31 32 // construct menu item, add to popup menu, enable event handling 33 for ( int count = 0; count < items.length; count++ ) 34 { 35 items[ count ] = new JRadioButtonMenuItem( colors[ count ] ); 36 popupMenu.add( items[ count ] ); // add item to pop-up menu 37 colorGroup.add( items[ count ] ); // add item to button group 38 items[ count ].addActionListener( handler ); // add handler 39 } // end for 40 41 setBackground( Color.WHITE ); // set background to white 42 43 // declare a MouseListener for the window to display pop-up menu 44 addMouseListener( 45 46 new MouseAdapter() // anonymous inner class 47 { 48 // handle mouse press event 49 public void mousePressed( MouseEvent event ) 50 { 51 checkForTriggerEvent( event ); // check for trigger 52 } // end method mousePressed 53 54 // handle mouse release event 55 public void mouseReleased( MouseEvent event ) 56 { 57 checkForTriggerEvent( event ); // check for trigger 58 } // end method mouseReleased 59 60 // determine whether event should trigger popup menu 61 private void checkForTriggerEvent( MouseEvent event ) 62 { 63 if ( event.isPopupTrigger() ) 64 popupMenu.show( 65 event.getComponent(), event.getX(), event.getY() ); 66 } // end method checkForTriggerEvent 67 } // end anonymous inner class 68 ); // end call to addMouseListener 69 } // end PopupFrame constructor 70 71 // private inner class to handle menu item events 72 private class ItemHandler implements ActionListener 73 { 74 // process menu item selections 75 public void actionPerformed( ActionEvent event ) 76 { 77 // determine which menu item was selected 78 for ( int i = 0 ; i < items.length; i++ ) 79 { 80 if ( event.getSource() == items[ i ] ) 81 { 82 getContentPane() .setBackground( colorValues[ i ] ); 83 return; 84 } // end if 85 } // end for 86 } // end method actionPerformed 87 } // end private inner class ItemHandler 88 } // end class PopupFrame |
Figure 22.8. Test class for PopupFrame.
(This item is displayed on page 1022 in the print version)
1 // Fig. 22.8: PopupTest.java 2 // Testing PopupFrame. 3 import javax.swing.JFrame; 4 5 public class PopupTest 6 { 7 public static void main( String args[] ) 8 { 9 PopupFrame popupFrame = new PopupFrame(); // create PopupFrame 10 popupFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 popupFrame.setSize( 300, 200 ); // set frame size 12 popupFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class PopupTest |
Line 25 of the PopupFrame constructor (lines 2169) creates an instance of class ItemHandler (declared in lines 7287) that will process the item events from the menu items in the pop-up menu. Line 29 creates the JPopupMenu. The for statement (lines 3339) creates a JRadioButtonMenuItem object (line 35), adds it to popupMenu (line 36), adds it to ButtonGroup colorGroup (line 37) to maintain one selected JRadioButtonMenuItem at a time and registers its ActionListener (line 38). Line 41 sets the initial background to white by invoking method setBackground.
Lines 4468 register a MouseListener to handle the mouse events of the application window. Methods mousePressed (lines 4952) and mouseReleased (lines 5558) check for the pop-up trigger event. Each method calls private utility method checkForTriggerEvent (lines 6166) to determine whether the pop-up trigger event occurred. If it did, MouseEvent method isPopupTrigger returns TRue, and JPopupMenu method show displays the JPopupMenu. The first argument to method show specifies the origin component, whose position helps determine where the JPopupMenu will appear on the screen. The last two arguments are the x-y coordinates (measured from the origin component's upper-left corner) at which the JPopupMenu is to appear.
Look-and-Feel Observation 22.10
Displaying a JPopupMenu for the pop-up trigger event of multiple GUI components requires registering mouse-event handlers for each of those GUI components. |
When the user selects a menu item from the pop-up menu, class ItemHandler's method actionPerformed (lines 7586) determines which JRadioButtonMenuItem the user selected and sets the background color of the window's content pane.
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover