MouseAdapter adapter = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ( e.isPopupTrigger() ) {
selectedComponent = getComponentAt( e.getX(), e.getY() );
colorMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
};
Button button = new Button("One");
button.addMouseListener( adapter );
add( button );
button = new Button("Two");
button.addMouseListener( adapter );
add( button );
button = new Button("Three");
button.addMouseListener( adapter );
add( button );
colorMenu = new PopupMenu("Color");
colorMenu.add( makeMenuItem("Red") );
colorMenu.add( makeMenuItem("Green") );
colorMenu.add( makeMenuItem("Blue") );
add(colorMenu);