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);
System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
if (e.isPopupTrigger()) { popup.show(this, e.getX(), e.getY()); }
JFrame frame = new JFrame("Popup Menu Example"); frame.addWindowListener(new BasicWindowMonitor()); frame.setContentPane(new PopupMenuExample()); frame.setSize(300, 300); frame.setVisible(true);
checkPopup(e);
System.out.println("Popup menu is hidden!");
System.out.println("Popup menu will be invisible!");
System.out.println("Popup menu will be visible!");