public class PopupFrame extends JFrame
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
89:
90: /**************************************************************************
91: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
92: * Pearson Education, Inc. All Rights Reserved. *
93: * *
94: * DISCLAIMER: The authors and publisher of this book have used their *
95: * best efforts in preparing the book. These efforts include the *
96: * development, research, and testing of the theories and programs *
97: * to determine their effectiveness. The authors and publisher make *
98: * no warranty of any kind, expressed or implied, with regard to these *
99: * programs or to the documentation contained in these books. The authors *
100: * and publisher shall not be liable in any event for incidental or *
101: * consequential damages in connection with, or arising out of, the *
102: * furnishing, performance, or use of these programs. *
103: *************************************************************************/
104: