Java Reference
In-Depth Information
This adds the element menu items to the pop-up. We could also add the font choice menu item but you
can't add a JMenuItem object to two different menus. You could either create an Action object that
would pop up the font dialog, or you could add a different menu item to the pop-up that did the same
thing when it was clicked.
Displaying a Pop-up Menu
You can display a pop-up within the coordinate system of any component, by calling the
show() method for the JPopupMenu object. The method requires three arguments to be specified, a
reference to the component that is the context for the pop-up, and the x and y coordinates where the
menu is to be displayed, relative to the origin of the parent. For example:
generalPopup.show(view, xCoord, yCoord);
This displays the pop-up at position ( xCoord , yCoord ) in the coordinate system for the component, view .
A pop-up menu is usually implemented as a context menu . The principal idea of a context menu is that
it's not just a single menu: it displays a different set of menu items depending on the context - that is,
what is under the mouse cursor when the button is pressed. The mouse button that you press to display
a context menu is sometimes called a pop-up trigger , simply because pressing it triggers the display of
the pop-up. On systems that support the notion of a pop-up trigger, the pop-up trigger is fixed, but it
can be different between systems. It is usually the right mouse button on a two- or three-button mouse,
and on systems with a one-button mouse, you typically have to hold down a modifier key while pressing
the mouse button.
The MouseEvent class has a special method, isPopupTrigger() , that returns true when the event
should display a pop-up menu. This method will only return true in the mousePressed() or
mouseReleased() methods. It will always return false in methods corresponding to other mouse
events. This method helps to get over the problem of different mouse buttons being used on different
systems to display a popup. If you use this method to decide when to display a popup, you've got them
covered - well, almost. You would typically use this with the following code to display a pop-up.
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
// Code to display the pop-up menu...
}
We have shown conceptual code for the mouseReleased() method here. This would be fine for
Windows but unfortunately it may not work on some other systems - Solaris for instance. This is
because in some operating system environments the isPopupTrigger() only returns true when the
button is pressed, not when it is released. The pop up trigger is not just a particular button - it is a
mouse-pressed or mouse-released event with a particular button. This implies that if you want your code
to work on a variety of systems using the 'standard' mouse button to trigger the pop-up in every case,
you must implement the code to call isPopupTrigger() and pop the menu in both the
mousePressed() and mouseReleased() methods. The method will only return true in one or the
other. Of course, you could always circumvent this by ignoring convention and pop the menu for a
specific button press with code like this:
Search WWH ::




Custom Search