Java Reference
In-Depth Information
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 parent
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 view component.
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. A context menu displays a different set of menu items depending on the context
— that is, what is under the mouse cursor when the button is clicked. 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 for right-
handed users. On systems with a one-button mouse, you typically have to hold down a modifier key while
pressing the mouse button to fire the pop-up trigger.
The MouseEvent class has a special method, isPopupTrigger() , that returns true when the event
should display a pop-up menu. This method returns true only in the mousePressed() or mouseReleased()
methods. It always returns false in methods responding to other mouse events. This method helps solve
the problem of different mouse buttons being used on different systems to display a pop-up. If you use this
method to decide when to display a pop-up, you have them covered — well, almost. You 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...
}
}
I 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 example. This is because in some
operating system environments, the isPopupTrigger() returns true only when the button is pressed, not
when it is released. The pop-up trigger is not just a particular button — it is a either a mouse-pressed event
or a mouse-released event associated 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 returns true only 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:
if((e.getButton() == e.BUTTON3) {
// Code to display the pop-up menu...
}
With this code, the pop-up operates only with button 3, regardless of the convention for the underlying oper-
ating system. However, the user might not be happy about having to use a different pop-up trigger for your
Java program compared to other applications on the same system.
Search WWH ::




Custom Search