samer@1: import java.awt.*; samer@1: import java.awt.event.*; samer@1: import javax.swing.JPopupMenu; samer@1: import javax.swing.JMenu; samer@1: import javax.swing.JMenuItem; samer@1: import javax.swing.JCheckBoxMenuItem; samer@1: import javax.swing.JRadioButtonMenuItem; samer@1: import javax.swing.ButtonGroup; samer@1: import javax.swing.JMenuBar; samer@1: import javax.swing.KeyStroke; samer@1: import javax.swing.ImageIcon; samer@1: samer@1: import javax.swing.JTextArea; samer@1: import javax.swing.JScrollPane; samer@1: import javax.swing.JFrame; samer@1: samer@1: /* samer@1: * This class adds popup menus to MenuDemo. samer@1: */ samer@1: public class PopupMenuDemo extends JFrame samer@1: implements ActionListener, ItemListener { samer@1: JTextArea output; samer@1: JScrollPane scrollPane; samer@1: String newline = "\n"; samer@1: JPopupMenu popup; samer@1: samer@1: public PopupMenuDemo() { samer@1: JMenuBar menuBar; samer@1: JMenu menu, submenu; samer@1: JMenuItem menuItem; samer@1: JRadioButtonMenuItem rbMenuItem; samer@1: JCheckBoxMenuItem cbMenuItem; samer@1: samer@1: addWindowListener(new WindowAdapter() { samer@1: public void windowClosing(WindowEvent e) { samer@1: System.exit(0); samer@1: } samer@1: }); samer@1: samer@1: //Add regular components to the window, using the default BorderLayout. samer@1: Container contentPane = getContentPane(); samer@1: output = new JTextArea(5, 30); samer@1: output.setEditable(false); samer@1: scrollPane = new JScrollPane(output); samer@1: contentPane.add(scrollPane, BorderLayout.CENTER); samer@1: samer@1: //Create the menu bar. samer@1: menuBar = new JMenuBar(); samer@1: setJMenuBar(menuBar); samer@1: samer@1: //Build the first menu. samer@1: menu = new JMenu("A Menu"); samer@1: menu.setMnemonic(KeyEvent.VK_A); samer@1: menu.getAccessibleContext().setAccessibleDescription( samer@1: "The only menu in this program that has menu items"); samer@1: menuBar.add(menu); samer@1: samer@1: //a group of JMenuItems samer@1: menuItem = new JMenuItem("A text-only menu item", samer@1: KeyEvent.VK_T); samer@1: //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead samer@1: menuItem.setAccelerator(KeyStroke.getKeyStroke( samer@1: KeyEvent.VK_1, ActionEvent.ALT_MASK)); samer@1: menuItem.getAccessibleContext().setAccessibleDescription( samer@1: "This doesn't really do anything"); samer@1: menuItem.addActionListener(this); samer@1: menu.add(menuItem); samer@1: menuItem = new JMenuItem("Both text and icon", samer@1: new ImageIcon("images/middle.gif")); samer@1: menuItem.setMnemonic(KeyEvent.VK_B); samer@1: menuItem.addActionListener(this); samer@1: menu.add(menuItem); samer@1: menuItem = new JMenuItem(new ImageIcon("images/middle.gif")); samer@1: menuItem.setMnemonic(KeyEvent.VK_D); samer@1: menuItem.addActionListener(this); samer@1: menu.add(menuItem); samer@1: samer@1: //a group of radio button menu items samer@1: menu.addSeparator(); samer@1: ButtonGroup group = new ButtonGroup(); samer@1: rbMenuItem = new JRadioButtonMenuItem("A radio button menu item"); samer@1: rbMenuItem.setSelected(true); samer@1: rbMenuItem.setMnemonic(KeyEvent.VK_R); samer@1: group.add(rbMenuItem); samer@1: rbMenuItem.addActionListener(this); samer@1: menu.add(rbMenuItem); samer@1: rbMenuItem = new JRadioButtonMenuItem("Another one"); samer@1: rbMenuItem.setMnemonic(KeyEvent.VK_O); samer@1: group.add(rbMenuItem); samer@1: rbMenuItem.addActionListener(this); samer@1: menu.add(rbMenuItem); samer@1: samer@1: //a group of check box menu items samer@1: menu.addSeparator(); samer@1: cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); samer@1: cbMenuItem.setMnemonic(KeyEvent.VK_C); samer@1: cbMenuItem.addItemListener(this); samer@1: menu.add(cbMenuItem); samer@1: cbMenuItem = new JCheckBoxMenuItem("Another one"); samer@1: cbMenuItem.setMnemonic(KeyEvent.VK_H); samer@1: cbMenuItem.addItemListener(this); samer@1: menu.add(cbMenuItem); samer@1: samer@1: //a submenu samer@1: menu.addSeparator(); samer@1: submenu = new JMenu("A submenu"); samer@1: submenu.setMnemonic(KeyEvent.VK_S); samer@1: samer@1: menuItem = new JMenuItem("An item in the submenu"); samer@1: menuItem.setAccelerator(KeyStroke.getKeyStroke( samer@1: KeyEvent.VK_2, ActionEvent.ALT_MASK)); samer@1: menuItem.addActionListener(this); samer@1: submenu.add(menuItem); samer@1: samer@1: menuItem = new JMenuItem("Another item"); samer@1: menuItem.addActionListener(this); samer@1: submenu.add(menuItem); samer@1: menu.add(submenu); samer@1: samer@1: //Build second menu in the menu bar. samer@1: menu = new JMenu("Another Menu"); samer@1: menu.setMnemonic(KeyEvent.VK_N); samer@1: menu.getAccessibleContext().setAccessibleDescription( samer@1: "This menu does nothing"); samer@1: menuBar.add(menu); samer@1: samer@1: //Create the popup menu. samer@1: popup = new JPopupMenu(); samer@1: menuItem = new JMenuItem("A popup menu item"); samer@1: menuItem.addActionListener(this); samer@1: popup.add(menuItem); samer@1: menuItem = new JMenuItem("Another popup menu item"); samer@1: menuItem.addActionListener(this); samer@1: popup.add(menuItem); samer@1: samer@1: //Add listener to components that can bring up popup menus. samer@1: MouseListener popupListener = new PopupListener(); samer@1: output.addMouseListener(popupListener); samer@1: scrollPane.addMouseListener(popupListener); samer@1: menuBar.addMouseListener(popupListener); samer@1: } samer@1: samer@1: public void actionPerformed(ActionEvent e) { samer@1: JMenuItem source = (JMenuItem)(e.getSource()); samer@1: String s = "Action event detected." samer@1: + newline samer@1: + " Event source: " + source.getText() samer@1: + " (an instance of " + getClassName(source) + ")"; samer@1: output.append(s + newline); samer@1: } samer@1: samer@1: public void itemStateChanged(ItemEvent e) { samer@1: JMenuItem source = (JMenuItem)(e.getSource()); samer@1: String s = "Item event detected." samer@1: + newline samer@1: + " Event source: " + source.getText() samer@1: + " (an instance of " + getClassName(source) + ")" samer@1: + newline samer@1: + " New state: " samer@1: + ((e.getStateChange() == ItemEvent.SELECTED) ? samer@1: "selected":"unselected"); samer@1: output.append(s + newline); samer@1: } samer@1: samer@1: // Returns just the class name -- no package info. samer@1: protected String getClassName(Object o) { samer@1: String classString = o.getClass().getName(); samer@1: int dotIndex = classString.lastIndexOf("."); samer@1: return classString.substring(dotIndex+1); samer@1: } samer@1: samer@1: public static void main(String[] args) { samer@1: PopupMenuDemo window = new PopupMenuDemo(); samer@1: samer@1: window.setTitle("PopupMenuDemo"); samer@1: window.setSize(450, 260); samer@1: window.setVisible(true); samer@1: } samer@1: samer@1: class PopupListener extends MouseAdapter { samer@1: public void mousePressed(MouseEvent e) { samer@1: maybeShowPopup(e); samer@1: } samer@1: samer@1: public void mouseReleased(MouseEvent e) { samer@1: maybeShowPopup(e); samer@1: } samer@1: samer@1: private void maybeShowPopup(MouseEvent e) { samer@1: if (e.isPopupTrigger()) { samer@1: popup.show(e.getComponent(), samer@1: e.getX(), e.getY()); samer@1: } samer@1: } samer@1: } samer@1: }