top examplets
Popup Menus
First the naive way I did it, then the nice way I saw posted on comp.lang.java.programmer by David Boydston.
Both ways work exactly the same from the user's point of view--They just handle mouse events differently.
If your browser supports Java 1.1, click here to see the applet run. Other browsers will do awful things.
// PopupMenuDemo1.java import java.applet.Applet; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; // Popup menu demo. Uses normal (delegation-based) JDK1.1 event handling, // which in this case is less than ideal because Win32 uses mouse release // to trigger popup menus and Solaris uses mouse down to trigger popup // menus. That forces us to test both mouse events for popup triggers. public class PopupMenuDemo1 extends Applet implements ActionListener { PopupMenu menu = new PopupMenu(); public void init() { MenuItem item; item = new MenuItem ("Foo"); item.addActionListener (this); menu.add (item); item = new MenuItem ("Bar"); item.addActionListener (this); menu.add (item); add (menu); addMouseListener ( new MouseAdapter() { public void mousePressed (MouseEvent e) {handleMousePressed (e);} public void mouseReleased (MouseEvent e) {handleMouseReleased (e);} } ); } // Called by the anonymous MouseAdapter to handle a mouse press. Popup // triggers occur here under Solaris. public void handleMousePressed (MouseEvent e) { if (e.isPopupTrigger() ) menu.show (this, e.getX(), e.getY() ); } // Called by the anonymous MouseAdapter to handle a mouse release. Popup // triggers occur here under Win32. public void handleMouseReleased (MouseEvent e) { if (e.isPopupTrigger() ) menu.show (this, e.getX(), e.getY() ); } // Someone clicked on one of the popup items. public void actionPerformed (ActionEvent e) { System.out.println (e.getActionCommand() ); } };
See also:
- JDK 1.1 Guide: Popup Menu
- API Reference: java.awt.MenuItem
- API Reference: java.awt.PopupMenu
- API Reference: java.awt.event.ActionListener
// PopupMenuDemo2.java // // Thanks to David Boydston < daveb@sc-systems.com> for posting the Usenet // message that made this examplet possible. import java.applet.Applet; import java.awt.AWTEvent; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; // Popup menu demo. Filter all mouse events for popup triggers. This is // much simpler than the naive way PopupMenuDemo1.jav did it. public class PopupMenuDemo2 extends Applet implements ActionListener { PopupMenu menu = new PopupMenu(); public void init() { MenuItem item; item = new MenuItem ("Foo"); item.addActionListener (this); menu.add (item); item = new MenuItem ("Bar"); item.addActionListener (this); menu.add (item); add (menu); enableEvents (AWTEvent.MOUSE_EVENT_MASK); } // Called for every mouse event. We need this since the popup triggers // occur during different mouse actions on different platforms // (MouseReleased on Win32, MousePressed on Solaris). public void processMouseEvent (MouseEvent e) { if (e.isPopupTrigger() ) menu.show (e.getComponent(), e.getX(), e.getY() ); super.processMouseEvent (e); } // Someone clicked on one of the popup items. public void actionPerformed (ActionEvent e) { System.out.println (e.getActionCommand() ); } };
See also
Copyright (c) 1997, 1998 by Wayne E. Conrad, All Rights Reserved
Last Updated May 6, 1998
This page has been accidentally visitedtimes since May 1st, 1998.
![]()
![]()
This page hosted by Get your own Free Home Page