FileDocCategorySizeDatePackage
PopupMenuExample.javaAPI DocExample2755Mon Nov 09 12:45:48 GMT 1998None

PopupMenuExample

public class PopupMenuExample extends JPanel implements MouseListener, ActionListener, PopupMenuListener

Fields Summary
public JPopupMenu
popup
Constructors Summary
public PopupMenuExample()


        popup = new JPopupMenu(); 

        JMenuItem item;
        popup.add(item = new JMenuItem("Left", new ImageIcon("left.gif")));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(this);
        popup.add(item = new JMenuItem("Center",
                                             new ImageIcon("center.gif")));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(this);
        popup.add(item = new JMenuItem("Right", new
                                                  ImageIcon("right.gif")));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(this);
        popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif")));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(this);
        popup.addSeparator();
        popup.add(item = new JMenuItem("Settings . . ."));
        item.addActionListener(this);

        popup.setLabel("Justification");
        popup.setBorder(new BevelBorder(BevelBorder.RAISED));
        popup.addPopupMenuListener(this);

        addMouseListener(this);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent event)

        System.out.println("Popup menu item [" + event.getActionCommand() +
                           "] was pressed.");
    
private voidcheckPopup(java.awt.event.MouseEvent e)

        if (e.isPopupTrigger()) {
            popup.show(this, e.getX(), e.getY());
        } 
    
public static voidmain(java.lang.String[] s)

        JFrame frame = new JFrame("Popup Menu Example");
        frame.addWindowListener(new BasicWindowMonitor());
        frame.setContentPane(new PopupMenuExample());
        frame.setSize(300, 300);
        frame.setVisible(true);
    
public voidmouseClicked(java.awt.event.MouseEvent e)

 checkPopup(e); 
public voidmouseEntered(java.awt.event.MouseEvent e)

public voidmouseExited(java.awt.event.MouseEvent e)

public voidmousePressed(java.awt.event.MouseEvent e)

 checkPopup(e); 
public voidmouseReleased(java.awt.event.MouseEvent e)

 checkPopup(e); 
public voidpopupMenuCanceled(javax.swing.event.PopupMenuEvent e)

        System.out.println("Popup menu is hidden!");
    
public voidpopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)

        System.out.println("Popup menu will be invisible!");
    
public voidpopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e)

        System.out.println("Popup menu will be visible!");