// // another example of the Java AWT, showing use of PopupMenu class. // see class definition and main method for details. // // this example is modified from a similar example in chapter 14 of // _Exploring Java_ (Niemeyer and Peck). // import java.awt.* ; import java.awt.event.* ; // // a PopupMenu object is a CloseableFrame containing two Button // objects. when the user triggers a popup menu (pressing the // right mouse button on Unix/X, pressing and releasing the // right mouse button on DOS/Windows), a PopupMenu appears that // allows the user to select a new color for the component (one of // the Buttons or the Frame) that contains the cursor. different // menus are provided for the Buttons and the Frame. // class PopupMenuExample extends CloseableFrame { PopupMenu buttonColorMenu ; PopupMenu frameColorMenu ; Component selectedC ; PopupMenuExample() { super("PopupMenuExample") ; setLayout(new FlowLayout()) ; add(makeButton("hello")) ; add(makeButton("goodbye")) ; // listener for selections from one of popup menus. ActionListener popupL = new ActionListener() { public void actionPerformed(ActionEvent e) { String s = e.getActionCommand() ; if (s.equals("pink")) selectedC.setBackground( Color.pink) ; else if (s.equals("cyan")) selectedC.setBackground( Color.cyan) ; else if (s.equals("yellow")) selectedC.setBackground( Color.yellow) ; else if (s.equals("white")) selectedC.setBackground( Color.white) ; // force repaint to pick up new // colors (seems to be necessary // for some DOS/Windows systems). repaint() ; } } ; // popup menu for Buttons. buttonColorMenu = new PopupMenu("Choose button color") ; buttonColorMenu.add(makePMenuItem("pink", popupL)) ; buttonColorMenu.add(makePMenuItem("cyan", popupL)) ; add(buttonColorMenu) ; // popup menu for Frame. frameColorMenu = new PopupMenu("Choose frame color") ; frameColorMenu.add(makePMenuItem("yellow", popupL)) ; frameColorMenu.add(makePMenuItem("white", popupL)) ; add(frameColorMenu) ; // listener for mouse operations -- processes requests // for popup menus. // some systems' popup menus are triggered by // mouse-press events, some by mouse-release // events, so must check both. MouseListener mouseL = new MouseAdapter() { public void mousePressed(MouseEvent e) { checkPopup(e) ; } public void mouseReleased(MouseEvent e) { checkPopup(e) ; } } ; // add listener to frame and all components it // contains. // code in _Exploring Java_ added listener to // Frame only, but this did not work for me, // at least under Unix/X. recurseAdd(this, mouseL) ; // _EJ_ approach: // addMouseListener(mouseL) ; pack() ; show() ; } // adds mouse listener to component and its contents. void recurseAdd(Component c, MouseListener l) { c.addMouseListener(l) ; if (c instanceof Container) { Component[] contents = ((Container) c).getComponents() ; for (int i = 0 ; i < contents.length ; i++) recurseAdd(contents[i], l) ; } } // checks for popup-menu trigger; if true, // decides which menu to pop up and displays it. // this is also modified from the example in _Exploring Java_, // since the method they use also did not work for me. void checkPopup(MouseEvent e) { if (e.isPopupTrigger()) { // _EJ_ approach: // selectedC = // getComponentAt(e.getX(), e.getY()) ; selectedC = (Component) e.getSource() ; if (selectedC instanceof Button) buttonColorMenu.show(e.getComponent(), e.getX(), e.getY()) ; else if (selectedC instanceof Frame) frameColorMenu.show(e.getComponent(), e.getX(), e.getY()) ; } } // --- methods to make menu items, buttons ---- MenuItem makePMenuItem(String label, ActionListener l) { MenuItem m = new MenuItem(label) ; m.addActionListener(l) ; return m ; } Button makeButton(final String label) { Button b = new Button(label) ; b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("button pressed: " + label) ; } } ) ; return b ; } // // ---- main --------------------------------------------------- // // no command-line arguments. // public static void main(String[] args) { PopupMenuExample m = new PopupMenuExample() ; } }