import java.awt.*; import java.awt.event.*; import javax.swing.JPopupMenu; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.JFrame; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; /* PopupMenuDemo.java requires images/middle.gif. */ /* * Like MenuDemo, but with popup menus added. */ public class PopupMenuDemo implements ActionListener, ItemListener { JTextArea output; JScrollPane scrollPane; String newline = "\n"; JMenu subMenu = new JMenu("Submenu"); PopupMenuListener l = new MyPopupMenuListener(); static PopupMenuDemo demo = new PopupMenuDemo(); JPopupMenu popup = new JPopupMenu(); public Container createContentPane() { //Create the content-pane-to-be. JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setOpaque(true); //Create a scrolled text area. output = new JTextArea(5, 30); output.setEditable(false); scrollPane = new JScrollPane(output); //Add the text area to the content pane. contentPane.add(scrollPane, BorderLayout.CENTER); return contentPane; } public void installPopupMenu() { //Add listener to the text area so the popup menu can come up. JMenuItem menuItem; //Create the popup menu. menuItem = new JMenuItem("A popup menu item"); menuItem.addActionListener(this); popup.add(menuItem); menuItem = new JMenuItem("Another popup menu item"); menuItem.addActionListener(this); popup.add(menuItem); MouseListener popupListener = new PopupListener(popup); output.addMouseListener(popupListener); } public void createPopupMenu() { popup.remove(subMenu); subMenu = new JMenu(); //Trying to add dummy menu item /*JMenuItem menuItem; menuItem = new JMenuItem("11111111111111111111 Dummy"); subMenu.add(menuItem);*/ popup.add(subMenu); subMenu.getPopupMenu().addPopupMenuListener(l); //fillSubmenu(); } private void fillSubmenu () { System.out.println("fillSubmenu"); subMenu.removeAll(); JMenuItem menuItem; menuItem = new JMenuItem("Item 1 Item 1 Item 1"); subMenu.add(menuItem); menuItem = new JMenuItem("Item 2 Item 2 Item 2"); subMenu.add(menuItem); } public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); String s = "Action event detected." + newline + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")"; output.append(s + newline); output.setCaretPosition(output.getDocument().getLength()); } public void itemStateChanged(ItemEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); String s = "Item event detected." + newline + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")" + newline + " New state: " + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"); output.append(s + newline); output.setCaretPosition(output.getDocument().getLength()); } // Returns just the class name -- no package info. protected String getClassName(Object o) { String classString = o.getClass().getName(); int dotIndex = classString.lastIndexOf("."); return classString.substring(dotIndex+1); } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = PopupMenuDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("PopupMenuDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create/set menu bar and content pane. frame.setContentPane(demo.createContentPane()); //Create and set up the popup menu. demo.installPopupMenu(); //Display the window. frame.setSize(450, 260); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } class PopupListener extends MouseAdapter { JPopupMenu popup; PopupListener(JPopupMenu popupMenu) { popup = popupMenu; } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { demo.createPopupMenu(); popup.show(e.getComponent(), e.getX(), e.getY()); } } } class MyPopupMenuListener implements PopupMenuListener { public void popupMenuWillBecomeVisible(PopupMenuEvent e) { fillSubmenu(); } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } public void popupMenuCanceled(PopupMenuEvent e) { } } }