import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; //Create a JPanel that handles a popup menu. Then use the JPanel as the //contentPane for a JFrame. public class PopupMenuExample extends JPanel implements ActionListener, PopupMenuListener { public JPopupMenu popup; public PopupMenuExample() { popup = new JPopupMenu(); //The popup menu object. //Assemble the popup menu. This is done exactly the same way as with a //non-popup menu. Actually, a "normal" menu is implemented as a button //that displays a popup menu when pressed. JMenuItem item; popup.add(item = new JMenuItem("Left", new ImageIcon("left.gif"))); //Place the text to the right of the icon. This will align the //icons properly. item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(this); popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif"))); //Place the text to the right of the icon. This will align the //icons properly. item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(this); popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif"))); //Place the text to the right of the icon. This will align the //icons properly. item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(this); popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif"))); //Place the text to the right of the icon. This will align the //icons properly. item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(this); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(this); popup.setLabel("Justification"); //Set the popup menus border. popup.setBorder(new BevelBorder(BevelBorder.RAISED)); //Listen for popup menus. This is not necessary. popup.addPopupMenuListener(this); //Display the popup menu when the mouse is released. addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { //The mouse event method isPopupTrigger() returns true if //this mouse event is this platforms popup trigger. For //Windows the popup trigger is the right mouse button. //Display the popup meny at the point where the mouse was released. //It is better to use the show() method instead of setVisible(true). //Normally, we do not have to take care of removing the popup menu. //This is handled by Swing. if (e.isPopupTrigger()) popup.show((Component)e.getSource(), e.getX(), e.getY()); } }); } public void popupMenuWillBecomeVisible(PopupMenuEvent e) { System.out.println("Popup menu will be visible!"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { System.out.println("Popup menu will be invisible!"); } public void popupMenuCanceled(PopupMenuEvent e) { System.out.println("Popup menu is hidden!"); } public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); } public static void main(String s[]) { JFrame frame = new JFrame("Popup Menu Example"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setContentPane(new PopupMenuExample()); frame.setSize(300, 300); frame.setVisible(true); } }