Java Reference
In-Depth Information
// Create the popup menu
makePopup ();
// Add the MouseAdapter that opens the popup.
canvas1.addMouseListener (fAdapter);
canvas2.addMouseListener (fAdapter);
}
// ctor
/** Create the popup menu and a MouseAdapter that opens
*itwhenright button (or equivalent) is clicked.
**/
void makePopup ()
{
fColorMenu
=
new JPopupMenu (
"
Color
"
);
fColorMenu.add (makeMenuItem (
"
Red
"
));
fColorMenu.add (makeMenuItem (
"
Green
"
));
fColorMenu.add (makeMenuItem (
"
Blue
"
));
// Create a MouseAdapter that creates a Popup menu
// when the right mouse or equivalent button clicked.
fAdapter
=
new MouseAdapter ()
{
// On some platforms, mouseReleased sets
// PopupTrigger.
public void mouseReleased (MouseEvent e)
{
if (e.isPopupTrigger ())
{
showPopupMenu (e);
}
}
// And on other platforms, mousePressed sets
// PopupTrigger.
public void mousePressed (MouseEvent e)
{
if (e.isPopupTrigger ())
{
showPopupMenu (e);
}
}
// Get the component over which the right button
// click occurred and show the menu there.
public void showPopupMenu (MouseEvent e)
{
fSelectedComponent
=
e.getComponent ();
fColorMenu.show (fSelectedComponent,
e.getX (), e.getY ());
}
}
;//anonymous MouseAdapter subclass
Search WWH ::
Custom Search