Part 8. Web Programming

 
[Page 974 ( continued )]

29.3. Popup Menus

A popup menu , also known as a context menu , is like a regular menu, but does not have a menu bar and can float anywhere on the screen. Creating a popup menu is similar to creating a regular menu. First, you create an instance of JPopupMenu , then you can add JMenuItem , JCheckBoxMenuItem , JRadioButtonMenuItem , and separators to the popup menu. For example, the following code creates a JPopupMenu and adds JMenuItems into it:

 JPopupMenu jPopupMenu =   new   JPopupMenu(); jPopupMenu.add(   new   JMenuItem(   "New"   )); jPopupMenu.add(   new   JMenuItem(   " Open "   )); 

A regular menu is always attached to a menu bar using the setJMenuBar method, but a popup menu is associated with a parent component and is displayed using the show method in the JPopupMenu class. You specify the parent component and the location of the popup menu, using the coordinate system of the parent like this:

 jPopupMenu.show(component, x, y); 

The popup menu usually contains the commands for an object. Customarily, you display a popup menu by pointing to the object and clicking a certain mouse button, the so-called popup trigger . Popup triggers are system-dependent. In Windows, the popup menu is displayed when the right-mouse button is released. In Motif, the popup menu is displayed when the third mouse button is pressed and held down.

Listing 29.2 gives an example that creates a text area in a scroll pane. When the mouse points to the text area, it triggers the popup menu display, as shown in Figure 29.4.

Figure 29.4. A popup menu is displayed when the popup trigger is issued on the text area.
(This item is displayed on page 975 in the print version)


Here are the major steps in the program (Listing 29.2):

1.
Create a popup menu using JPopupMenu . Create menu items for New, Open, Print, and Exit using JMenuItem . For the menu items with both labels and icons, it is convenient to use the JMenuItem(label, icon) constructor.

2.
Add the menu items into the popup menu.


[Page 975]
3.
Create a scroll pane and add a text area into it. Place the scroll pane in the center of the applet.

4.
Implement the actionPerformed handler to process the events from the menu items.

5.
Implement the mousePressed and mouseReleased methods to process the events for handling popup triggers.

Listing 29.2. PopupMenuDemo.java
(This item is displayed on pages 975 - 976 in the print version)
 1   import   javax.swing.*; 2   import   java.awt.*; 3   import   java.awt.event.*; 4 5   public class   PopupMenuDemo   extends   JApplet { 6    private   JPopupMenu jPopupMenu1 =   new   JPopupMenu();  7   private   JMenuItem jmiNew =   new   JMenuItem(   "New"   , 8   new   ImageIcon(getClass().getResource(   "image/new.gif"   ))); 9   private   JMenuItem jmiOpen =   new   JMenuItem(   "Open"   , 10   new   ImageIcon(getClass().getResource(   "image/open.gif"   ))); 11   private   JMenuItem jmiPrint =   new   JMenuItem(   "Print"   , 12   new   ImageIcon(getClass().getResource(   "image/print.gif"   ))); 13   private   JMenuItem jmiExit =   new   JMenuItem(   "Exit"   ); 14   private   JTextArea jTextArea1 =   new   JTextArea(); 15 16   public   PopupMenuDemo() { 17  jPopupMenu1.add(jmiNew);  18 jPopupMenu1.add(jmiOpen); 19 jPopupMenu1.addSeparator(); 20 jPopupMenu1.add(jmiPrint); 21 jPopupMenu1.addSeparator(); 22 jPopupMenu1.add(jmiExit); 23 jPopupMenu1.add(jmiExit); 24 25 add(   new   JScrollPane(jTextArea1), BorderLayout.CENTER); 26 27  jmiNew.addActionListener(   new   ActionListener() {  28   public void   actionPerformed(ActionEvent e) { 29 System.out.println(   "Process New"   ); 30 } 31 }); 32  jmiOpen.addActionListener(   new   ActionListener() {  33   public void   actionPerformed(ActionEvent e) { 34 System.out.println(   "Process Open"   ); 35 } 36 }); 37  jmiPrint.addActionListener(   new   ActionListener() {  38   public void   actionPerformed(ActionEvent e) { 39 System.out.println(   "Process Print"   ); 40 } 

[Page 976]
 41 }); 42  jmiExit.addActionListener(   new   ActionListener() {  43   public void   actionPerformed(ActionEvent e) { 44 System.exit(     ); 45 } 46 }); 47  jTextArea1.addMouseListener(   new   MouseAdapter() {  48   public void   mousePressed(MouseEvent e) {  // For Motif  49  showPopup(e);  50 } 51 52   public void   mouseReleased(MouseEvent e) {  // For Windows  53 showPopup(e); 54 } 55 }); 56 } 57 58  /** Display popup menu when triggered */  59   private void   showPopup(java.awt.event.MouseEvent evt) { 60   if   (  evt.isPopupTrigger()  ) 61  jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());  62 } 63 } 

The process of creating popup menus is similar to the process for creating regular menus. To create a popup menu, create a JPopupMenu as the basis (line 6), and add JMenuItem s to the popup menu (lines 17 “23).

To show a popup menu, use the show method by specifying the parent component and the location for the popup menu (line 47). The show method is invoked when the popup menu is triggered by a particular mouse click on the text area. Popup triggers are system-dependent. The listener implements the mouseReleased handler for displaying the popup menu in Windows (lines 52 “54) and the mousePressed handler for displaying the popup menu in Motif (lines 48 “50).

Tip

JDK 1.5 provides a new setComponentPopupMenu(JPopupMenu) method in the JComponent class, which can be used to add a popup menu on a component. This method automatically handles mouse listener registration and popup display. Using this method, you may delete the showPopup method in lines 59 “62 and replace the code in lines 47 “55 with the following statement:

 jTextArea1.setComponentPopupMenu(jPopupMenu1); 

Due to a bug, this method does not work with JPanel .


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net