Example usage for javax.swing JPopupMenu add

List of usage examples for javax.swing JPopupMenu add

Introduction

In this page you can find the example usage for javax.swing JPopupMenu add.

Prototype

public JMenuItem add(Action a) 

Source Link

Document

Appends a new menu item to the end of the menu which dispatches the specified Action object.

Usage

From source file:MyPopupMenuListener.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu popupMenu = new JPopupMenu("Title");
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();

    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    popupMenu.add(copyMenuItem);//from   w w  w.ja  v  a 2 s  . c  o  m

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener actionListener = new PopupActionListener();
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();

    JPopupMenu popupMenu = new JPopupMenu("Title");
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);//ww  w.j ava  2s .c  o m

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MyPopupMenuListener.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("PopupSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create popup menu, attach popup menu listener
    JPopupMenu popupMenu = new JPopupMenu("Title");
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();

    popupMenu.addPopupMenuListener(popupMenuListener);

    // Cut/*from   w  ww. j a  va 2  s .  com*/
    JMenuItem cutMenuItem = new JMenuItem("Cut");
    popupMenu.add(cutMenuItem);

    // Copy
    JMenuItem copyMenuItem = new JMenuItem("Copy");
    popupMenu.add(copyMenuItem);
    // Paste
    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    // Separator
    popupMenu.addSeparator();

    // Find
    JMenuItem findMenuItem = new JMenuItem("Find");
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:PopupSample.java

public static void main(String args[]) {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + actionEvent.getActionCommand());
        }/*  www .  j  a va  2s .  c  o  m*/
    };

    PopupMenuListener popupMenuListener = new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
            System.out.println("Canceled");
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Invisible");
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Visible");
        }
    };

    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);

    MouseListener mouseListener = new JPopupMenuShower(popupMenu);
    frame.addMouseListener(mouseListener);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:PopupMenu.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JPopupMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Toolkit toolkit = frame.getToolkit();

    final JPopupMenu menu = new JPopupMenu();
    JMenuItem menuItemBeep = new JMenuItem("Beep");

    menuItemBeep.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toolkit.beep();/*from   w w w . j a  v a  2  s . c o  m*/
        }
    });

    menu.add(menuItemBeep);

    JMenuItem menuItemClose = new JMenuItem("Close");
    menuItemClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    menu.add(menuItemClose);
    frame.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if (e.getButton() == e.BUTTON3) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MediumPopupMenuSample.java

public static void main(String args[]) {
    // Define ActionListener
    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Selected: " + event.getActionCommand());
        }//from  w  w  w  .j  av  a 2s .com
    };

    // Define PopupMenuListener
    PopupMenuListener pListener = new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent event) {
            System.out.println("Canceled");
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent event) {
            System.out.println("Becoming Invisible");
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent event) {
            System.out.println("Becoming Visible");
        }
    };

    // Define
    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu.setDefaultLightWeightPopupEnabled(false);

    // Create popup menu, attach popup menu listener
    final JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.addPopupMenuListener(pListener);

    // Cut
    JMenuItem cutItem = new JMenuItem("Cut");
    cutItem.addActionListener(aListener);
    popupMenu.add(cutItem);

    // Copy
    JMenuItem copyItem = new JMenuItem("Copy");
    copyItem.addActionListener(aListener);
    popupMenu.add(copyItem);

    // Paste
    JMenuItem pasteItem = new JMenuItem("Paste");
    pasteItem.addActionListener(aListener);
    pasteItem.setEnabled(false);
    popupMenu.add(pasteItem);

    // Separator
    popupMenu.addSeparator();

    // Find
    JMenuItem findItem = new JMenuItem("Find");
    findItem.addActionListener(aListener);
    popupMenu.add(findItem);

    // Enable showing
    MouseListener mouseListener = new JPopupMenuShower(popupMenu);
    frame.addMouseListener(mouseListener);

    Button button = new Button("Label");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(popupMenu.isLightWeightPopupEnabled());
        }
    });
    frame.getContentPane().add(button, BorderLayout.SOUTH);

    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:GUI.MainWindow.java

/**
 * @param args the command line arguments
 *//*  w w w.  j  a  v  a  2s .  co  m*/
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {

        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    }
    //</editor-fold>

    // Create right click context menu for most Text Components
    final JPopupMenu copypaste = new JPopupMenu();

    JMenuItem cut = new JMenuItem(new DefaultEditorKit.CutAction());
    cut.setText("Cut");
    copypaste.add(cut);

    JMenuItem copy = new JMenuItem(new DefaultEditorKit.CopyAction());
    copy.setText("Copy");
    copypaste.add(copy);

    JMenuItem paste = new JMenuItem(new DefaultEditorKit.PasteAction());
    paste.setText("Paste");
    copypaste.add(paste);

    // Taken from here. It succinctly added a right click context menu to every text component.
    // http://stackoverflow.com/questions/19424574/adding-a-context-menu-to-all-swing-text-components-in-application
    javax.swing.UIManager.addAuxiliaryLookAndFeel(new LookAndFeel() {
        private final UIDefaults defaults = new UIDefaults() {
            public javax.swing.plaf.ComponentUI getUI(JComponent c) {
                if (c instanceof javax.swing.text.JTextComponent) {
                    if (c.getClientProperty(this) == null) {
                        c.setComponentPopupMenu(copypaste);
                        c.putClientProperty(this, Boolean.TRUE);
                    }
                }
                return null;
            }
        };

        @Override
        public UIDefaults getDefaults() {
            return defaults;
        }

        ;

        @Override
        public String getID() {
            return "TextContextMenu";
        }

        @Override
        public String getName() {
            return getID();
        }

        @Override
        public String getDescription() {
            return getID();
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }
    });

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainWindow().setVisible(true);
        }
    });
}

From source file:Main.java

/**
 * Add a menu item to a JPopupMenu attaching listener to it
 * //from  w w w.j a v a2s. co  m
 * @param menu
 * @param label
 * @param listener
 */
public static void addMenuItem(JPopupMenu menu, String label, ActionListener listener) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(listener);
    menu.add(item);
}

From source file:com.intuit.tank.tools.debugger.PanelBuilder.java

/**
 * @param debuggerActions//from   w w  w.  j a va  2 s .  c om
 * @param loadChooser
 * @return
 */
static Component createBottomPanel(final AgentDebuggerFrame frame) {
    JPanel panel = new JPanel(new BorderLayout());

    RSyntaxTextArea loggerTA = new RSyntaxTextArea();
    frame.setLoggerTA(loggerTA);
    loggerTA.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
    loggerTA.setHyperlinksEnabled(false);
    loggerTA.setEditable(false);
    loggerTA.setHighlightCurrentLine(false);
    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.add(frame.getDebuggerActions().getSaveLogAction());
    popupMenu.add(frame.getDebuggerActions().getClearLogOutputAction());
    loggerTA.setPopupMenu(popupMenu);

    RTextScrollPane sp = new RTextScrollPane(loggerTA);
    sp.setIconRowHeaderEnabled(false);
    sp.getGutter().setBookmarkingEnabled(false);

    panel.add(sp, BorderLayout.CENTER);

    Logger root = Logger.getRootLogger();
    root.addAppender(new DebuggerAppender(loggerTA));
    root.setLevel(Level.INFO);
    return panel;
}

From source file:Main.java

/**
 * Make a popu menu using lists of actions. Each action list will be separated by
 * a menu separator. Empty action lists, or null-valued action lists will be ignored as if
 * the were not there./* w w  w  .  j  ava 2  s  . com*/
 * @param aActionLists
 * @return A popup menu.
 */
public static JPopupMenu popupBuilder(Action[]... aActionLists) {
    // Prepare the popup.
    final JPopupMenu lMenu = new JPopupMenu();

    // First we are going to filter out the empty lists.
    final java.util.List<Action[]> lActionList = new ArrayList<Action[]>();
    for (Action[] lList : aActionLists)
        if (lList != null && lList.length > 0)
            lActionList.add(lList);

    // Build the popup menu.
    for (int i = 0; i < lActionList.size(); i++) {
        for (Action lAction : lActionList.get(i))
            lMenu.add(lAction);
        if (i < lActionList.size() - 1)
            lMenu.addSeparator();
    }
    return lMenu;
}