![]() |
The Java Developers Almanac 1.4 |
|
e810. Creating a Popup Menu final JPopupMenu menu = new JPopupMenu();
// Create and add a menu item
JMenuItem item = new JMenuItem("Item Label");
item.addActionListener(actionListener);
menu.add(item);
// Set the component to show the popup menu
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
public void mouseReleased(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
});
e809. Separating Menu Items in a Menu e811. Creating a Popup Menu with Nested Menus e812. Forcing a Popup Menu to Be a Heavyweight Component e813. Getting the Currently Selected Menu or Menu Item e814. Creating a Menu Item That Listens for Changes to Its Selection Status e815. Listening for Changes to the Currently Selected Menu or Menu Item © 2002 Addison-Wesley. |