Example usage for javax.swing.event MenuListener MenuListener

List of usage examples for javax.swing.event MenuListener MenuListener

Introduction

In this page you can find the example usage for javax.swing.event MenuListener MenuListener.

Prototype

MenuListener

Source Link

Usage

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);/*w ww . j  a v  a 2 s.c om*/

    MenuListener lis = new MenuListener() {

        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
        }

        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");

        }

        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");

        }
    };

    fileMenu.addMenuListener(lis);

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);

    fileMenu.removeMenuListener(lis);
}

From source file:ContructMenuWithAction.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//from  w ww . j a  v a 2 s .  c o m

    fileMenu.addMenuListener(new MenuListener() {

        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
        }

        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");

        }

        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");

        }
    });

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);

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

From source file:MenuTest.java

public MenuTest() {
    super();/*from w w w  . j a va2  s  . c o m*/

    MenuListener listener = new MenuListener() {
        public void menuCanceled(MenuEvent e) {
            dumpInfo("Canceled", e);
        }

        public void menuDeselected(MenuEvent e) {
            dumpInfo("Deselected", e);
        }

        public void menuSelected(MenuEvent e) {
            dumpInfo("Selected", e);
        }

        private void dumpInfo(String s, MenuEvent e) {
            JMenu menu = (JMenu) e.getSource();
            System.out.println(s + ": " + menu.getText());
        }
    };

    JMenu fileMenu = new JMenu("File");
    fileMenu.addMenuListener(listener);
    fileMenu.add(new JMenuItem("Open"));
    fileMenu.add(new JMenuItem("Close"));
    fileMenu.add(new JMenuItem("Exit"));
    JMenu helpMenu = new JMenu("Help");
    helpMenu.addMenuListener(listener);
    helpMenu.add(new JMenuItem("About MenuTest"));
    helpMenu.add(new JMenuItem("Class Hierarchy"));
    helpMenu.addSeparator();
    helpMenu.add(new JCheckBoxMenuItem("Balloon Help"));
    JMenu subMenu = new JMenu("Categories");
    subMenu.addMenuListener(listener);
    JRadioButtonMenuItem rb;
    ButtonGroup group = new ButtonGroup();
    subMenu.add(rb = new JRadioButtonMenuItem("A Little Help", true));
    group.add(rb);
    subMenu.add(rb = new JRadioButtonMenuItem("A Lot of Help"));
    group.add(rb);
    helpMenu.add(subMenu);
    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(helpMenu);
    setJMenuBar(mb);
}

From source file:com.haulmont.cuba.desktop.sys.MenuBuilder.java

private void createMenuBarItem(JMenuBar menuBar, MenuItem item) {
    String caption = menuConfig.getItemCaption(item.getId());
    if (!item.getChildren().isEmpty() || item.isMenu()) {
        final JMenu jMenu = new JMenu(caption);
        jMenu.addMenuListener(new MenuListener() {
            @Override/*from w w w . ja v a 2 s  .  c o m*/
            public void menuSelected(MenuEvent e) {
                jMenu.requestFocus();
            }

            @Override
            public void menuDeselected(MenuEvent e) {
            }

            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });
        jMenu.setName(item.getId());
        menuBar.add(jMenu);
        createSubMenu(jMenu, item);
    } else {
        JMenuItem jMenuItem = new JMenuItem(caption);
        jMenuItem.setName(item.getId());
        //todo remove hardcoded border
        jMenuItem.setBorder(BorderFactory.createEmptyBorder(1, 4, 2, 4));
        assignShortcut(jMenuItem, item);
        jMenuItem.setMaximumSize(
                new Dimension(jMenuItem.getPreferredSize().width, jMenuItem.getMaximumSize().height));
        assignCommand(jMenuItem, item);
        menuBar.add(jMenuItem);
    }
}

From source file:gdt.jgui.entity.bonddetail.JAddDetailPanel.java

/**
 * Get context menu./*  www .j a  va 2 s  .c o  m*/
 * @return the context menu.
 * 
 */
@Override
public JMenu getContextMenu() {
    menu = super.getContextMenu();
    mia = null;
    int cnt = menu.getItemCount();
    if (cnt > 0) {
        mia = new JMenuItem[cnt];
        for (int i = 0; i < cnt; i++)
            mia[i] = menu.getItem(i);
    }
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            menu.removeAll();
            if (mia != null)
                for (JMenuItem mi : mia)
                    try {
                        if (mi != null)
                            menu.add(mi);
                    } catch (Exception ee) {
                        System.out.println("JAddDetailPanel:getConextMenu:" + ee.toString());
                    }

            JMenuItem doneItem = new JMenuItem("Done");
            doneItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (requesterResponseLocator$ != null) {
                        try {
                            byte[] ba = Base64.decodeBase64(requesterResponseLocator$);
                            String responseLocator$ = new String(ba, "UTF-8");
                            JConsoleHandler.execute(console, responseLocator$);
                        } catch (Exception ee) {
                            LOGGER.severe(ee.toString());
                        }
                    } else
                        console.back();

                }

            });
            add(doneItem);

            if (hasSelectedItems()) {

                addItem = new JMenuItem("Add");
                addItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //removeComponents();
                    }
                });
                menu.add(addItem);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });
    return menu;
}

From source file:gdt.jgui.entity.JEntitiesPanel.java

/**
 * Get context menu./*  w  ww .j a va 2s  .  c  o m*/
 * @return the context menu.
 * 
 */
@Override
public JMenu getContextMenu() {
    menu = super.getContextMenu();
    mia = null;
    int cnt = menu.getItemCount();
    if (cnt > 0) {
        mia = new JMenuItem[cnt];
        for (int i = 0; i < cnt; i++)
            mia[i] = menu.getItem(i);
    }
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            menu.removeAll();
            if (mia != null)
                for (JMenuItem mi : mia)
                    try {
                        if (mi != null)
                            menu.add(mi);
                    } catch (Exception ee) {
                        System.out.println("JEntitiesPanel:getConextMenu:" + ee.toString());
                    }
            Properties locator = Locator.toProperties(locator$);
            if (locator.getProperty(EntityHandler.ENTITY_CONTAINER) != null) {
                if (JEntityPrimaryMenu.hasToPaste(console, locator$)) {
                    pasteItem = new JMenuItem("Paste components");
                    pasteItem.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            pasteComponents();
                        }
                    });
                    menu.add(pasteItem);
                }
                if (hasSelectedItems()) {
                    if (containerKey$ != null) {
                        removeComponentsItem = new JMenuItem("Remove components");
                        removeComponentsItem.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                removeComponents();
                            }
                        });
                        menu.add(removeComponentsItem);
                    }
                }
            }
            if (locator.getProperty(EntityHandler.ENTITY_COMPONENT) != null) {
                if (componentKey$ != null) {
                    removeContainersItem = new JMenuItem("Remove containers");
                    removeContainersItem.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            removeContainers();
                        }
                    });
                    menu.add(removeContainersItem);
                }
            }
            if (hasSelectedItems()) {
                menu.addSeparator();
                copyItem = new JMenuItem("Copy");
                copyItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JItemPanel[] ipa = JEntitiesPanel.this.getItems();
                        ArrayList<String> sl = new ArrayList<String>();
                        for (JItemPanel ip : ipa)
                            if (ip.isChecked())
                                sl.add(ip.getLocator());
                        String[] sa = sl.toArray(new String[0]);
                        console.clipboard.clear();
                        for (String aSa : sa)
                            console.clipboard.putString(aSa);
                    }
                });
                menu.add(copyItem);
                reindexItem = new JMenuItem("Reindex");
                reindexItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JItemPanel[] ipa = JEntitiesPanel.this.getItems();
                        for (JItemPanel ip : ipa)
                            if (ip.isChecked()) {
                                JEntityPrimaryMenu.reindexEntity(console, ip.getLocator());
                            }
                    }
                });
                menu.add(reindexItem);
                archiveItem = new JMenuItem("Archive");
                archiveItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JItemPanel[] ipa = JEntitiesPanel.this.getItems();
                        Entigrator entigrator = console.getEntigrator(entihome$);
                        Properties locator;
                        ArrayList<String> sl = new ArrayList<String>();
                        String entityKey$;
                        for (JItemPanel ip : ipa) {
                            if (ip.isChecked()) {
                                locator = Locator.toProperties(ip.getLocator());
                                entityKey$ = locator.getProperty(EntityHandler.ENTITY_KEY);
                                sl.add(entityKey$);
                            }
                        }
                        String[] sa = sl.toArray(new String[0]);
                        System.out.println("JEntitiesPanel:archive:1");
                        String[] ea = JReferenceEntry.getCoalition(console, entigrator, sa);
                        if (ea == null)
                            System.out.println("JEntitiesPanel:archive:ea null");
                        else
                            System.out.println("JEntitiesPanel:archive:ea=" + ea.length);
                        JArchivePanel archivePanel = new JArchivePanel();
                        String apLocator$ = archivePanel.getLocator();
                        //locator$=getLocator();
                        apLocator$ = Locator.append(apLocator$, Entigrator.ENTIHOME, entihome$);
                        apLocator$ = Locator.append(apLocator$, EntityHandler.ENTITY_LIST,
                                Locator.toString(ea));
                        String icon$ = Support.readHandlerIcon(null, JEntityPrimaryMenu.class, "archive.png");
                        apLocator$ = Locator.append(apLocator$, Locator.LOCATOR_ICON, icon$);
                        JConsoleHandler.execute(console, apLocator$);
                    }
                });
                menu.add(archiveItem);
                menu.addSeparator();
                deleteItem = new JMenuItem("Delete");
                deleteItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int response = JOptionPane.showConfirmDialog(console.getContentPanel(), "Delete ?",
                                "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (response == JOptionPane.YES_OPTION) {
                            JItemPanel[] ipa = JEntitiesPanel.this.getItems();
                            Entigrator entigrator = console.getEntigrator(entihome$);
                            String iLocator$;
                            Properties iLocator;
                            String iEntityKey$;
                            String iEntityLabel$;
                            Sack iEntity;
                            ArrayList<String> sl = new ArrayList<String>();
                            for (JItemPanel ip : ipa) {
                                iLocator$ = ip.getLocator();
                                iLocator = Locator.toProperties(iLocator$);
                                iEntityLabel$ = iLocator.getProperty(EntityHandler.ENTITY_LABEL);
                                if (ip.isChecked()) {
                                    iEntityKey$ = iLocator.getProperty(EntityHandler.ENTITY_KEY);
                                    iEntity = entigrator.getEntityAtKey(iEntityKey$);
                                    if (iEntity != null)
                                        entigrator.deleteEntity(iEntity);
                                } else {
                                    sl.add(iEntityLabel$);
                                }
                            }
                            String[] sa = sl.toArray(new String[0]);
                            if (sa != null && sa.length > 0) {
                                String sa$ = Locator.toString(sa);
                                locator$ = Locator.append(locator$, EntityHandler.ENTITY_LIST, sa$);
                            }
                            JConsoleHandler.execute(console, locator$);
                        }
                    }
                });
                menu.add(deleteItem);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });
    menu.addSeparator();
    JMenuItem doneItem = new JMenuItem("Done");
    doneItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (requesterResponseLocator$ != null) {
                try {
                    byte[] ba = Base64.decodeBase64(requesterResponseLocator$);
                    String responseLocator$ = new String(ba, "UTF-8");
                    JConsoleHandler.execute(console, responseLocator$);
                } catch (Exception ee) {
                    LOGGER.severe(ee.toString());
                }
            } else
                console.back();
        }
    });
    menu.add(doneItem);
    return menu;
}

From source file:gdt.jgui.entity.JEntityPrimaryMenu.java

/**
 * Get context menu./* www . ja  v a 2s  .c  om*/
 * @return the context menu.
 */
@Override

public JMenu getContextMenu() {
    menu = new JMenu("Context");
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            JMenuItem doneItem = new JMenuItem("Done");
            doneItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (requesterResponseLocator$ != null) {
                        try {
                            byte[] ba = Base64.decodeBase64(requesterResponseLocator$);
                            String responseLocator$ = new String(ba, "UTF-8");
                            JConsoleHandler.execute(console, responseLocator$);
                        } catch (Exception ee) {
                            LOGGER.severe(ee.toString());
                        }
                    } else
                        console.back();
                }
            });
            menu.add(doneItem);
            if (hasToPaste()) {
                JMenuItem pasteItem = new JMenuItem("Paste components");
                pasteItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pasteComponents();
                    }
                });
                menu.add(pasteItem);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void menuCanceled(MenuEvent e) {
            // TODO Auto-generated method stub

        }

    });

    return menu;
}

From source file:net.brtly.monkeyboard.gui.MasterControlPanel.java

public MasterControlPanel(JFrame frame) {
    _frame = frame;// ww  w  . j  av a 2s.com
    initWindowListener(frame);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnActions = new JMenu("Actions");
    menuBar.add(mnActions);

    JMenu mnDebug = new JMenu("Debug");
    mnActions.add(mnDebug);

    JMenuItem mntmAddPluginpanel = new JMenuItem("Request null PluginPanel");
    mntmAddPluginpanel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Requesting null PluginPanel");
            // TODO
        }
    });
    mnDebug.add(mntmAddPluginpanel);

    JMenu mnOptions = new JMenu("Options");
    menuBar.add(mnOptions);

    _viewMenu = new JMenu("Views");

    _viewMenu.addMenuListener(new MenuListener() {

        @Override
        public void menuCanceled(MenuEvent arg0) {
        }

        @Override
        public void menuDeselected(MenuEvent arg0) {
        }

        @Override
        public void menuSelected(MenuEvent arg0) {
            updateViewMenu();
        }

    });
    menuBar.add(_viewMenu);

    // INITIALIZE MANAGERS
    // TODO: maybe call this in the Runnable and fire an event when finished
    _eventBus = new SwingEventBus();
    _eventBus.register(this);
    DeviceManager.init(_eventBus);

    _pluginManager = new PluginManager(_eventBus);
    _pluginManager.loadPlugins();

    // create the status bar panel and shove it down the bottom of the frame
    statusPanel = new StatusBar(frame);
    _frame.getContentPane().add(statusPanel, BorderLayout.SOUTH);

    _dockController = new CControl(frame);
    _frame.getContentPane().add(_dockController.getContentArea(), BorderLayout.CENTER);
    _panelFactory = new PluginPanelDockableFactory(_pluginManager);
    _dockController.addMultipleDockableFactory(PluginPanelDockableFactory.ID, _panelFactory);
    _dockController.createWorkingArea("root");

    updateViewMenu();

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            DeviceManager.start(null);
        }
    });

    _runOnClose.add(new Runnable() {
        public void run() {
            _dockController.destroy();
            DeviceManager.stop();
            DeviceManager.shutdown();
            System.exit(0);
        }
    });
}

From source file:gdt.jgui.entity.bookmark.JBookmarksEditor.java

/**
 * Get context menu.//from w w  w .  j  ava 2s.  c  o m
 * @return the context menu.
 */
@Override
public JMenu getContextMenu() {
    menu = super.getContextMenu();
    mia = null;
    int cnt = menu.getItemCount();
    if (cnt > 0) {
        mia = new JMenuItem[cnt];
        for (int i = 0; i < cnt; i++)
            mia[i] = menu.getItem(i);
    }
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            //            System.out.println("BookmarksEditor:getConextMenu:menu selected");
            menu.removeAll();
            if (mia != null) {
                for (JMenuItem mi : mia)
                    menu.add(mi);
            }
            if (hasSelectedItems()) {
                menu.addSeparator();
                JMenuItem copyItem = new JMenuItem("Copy");
                copyItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        copy();
                    }
                });
                menu.add(copyItem);
                JMenuItem deleteItem = new JMenuItem("Delete");
                deleteItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            int response = JOptionPane.showConfirmDialog(console.getContentPanel(), "Delete ?",
                                    "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                            if (response == JOptionPane.YES_OPTION) {
                                String[] sa = JBookmarksEditor.this.listSelectedItems();
                                if (sa == null)
                                    return;
                                String bookmarkKey$;
                                Entigrator entigrator = console.getEntigrator(entihome$);
                                Sack entity = entigrator.getEntityAtKey(entityKey$);
                                for (String aSa : sa) {
                                    bookmarkKey$ = Locator.getProperty(aSa, BOOKMARK_KEY);
                                    if (bookmarkKey$ == null)
                                        continue;
                                    entity.removeElementItem("jbookmark", bookmarkKey$);
                                }
                                entigrator.save(entity);
                                JConsoleHandler.execute(console, getLocator());
                            }
                        } catch (Exception ee) {
                            LOGGER.severe(ee.toString());
                        }
                    }
                });
                menu.add(deleteItem);
            }
            if (hasToPaste()) {
                JMenuItem pasteItem = new JMenuItem("Paste");
                pasteItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        paste();
                    }
                });
                menu.add(pasteItem);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });
    return menu;
}

From source file:gdt.jgui.tool.JTextEncrypter.java

/**
 * Get context menu.//  w ww  . j  av a  2  s . c  om
 * @return the context menu. 
 */
@Override
public JMenu getContextMenu() {
    menu = new JMenu("Context");
    menu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            menu.removeAll();
            JMenuItem doneItem = new JMenuItem("Done");
            doneItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (requesterResponseLocator$ != null) {
                        try {
                            byte[] ba = Base64.decodeBase64(requesterResponseLocator$);
                            String responseLocator$ = new String(ba, "UTF-8");
                            text$ = textArea.getText();
                            responseLocator$ = Locator.append(responseLocator$, JTextEditor.TEXT, text$);
                            //System.out.println("TextEditor:done:response locator="+responseLocator$);
                            JConsoleHandler.execute(console, responseLocator$);
                        } catch (Exception ee) {
                            Logger.getLogger(JTextEncrypter.class.getName()).severe(ee.toString());
                        }
                    } else
                        console.back();

                }
            });
            menu.add(doneItem);
            JMenuItem cancelItem = new JMenuItem("Cancel");
            cancelItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    console.back();
                }
            });
            menu.add(cancelItem);
            char[] master = passwordField.getPassword();
            //            System.out.println("Textencoder:context menu:master="+master.length);
            if (master.length > 6) {
                menu.addSeparator();
                JMenuItem encryptItem = new JMenuItem("Encrypt");
                encryptItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        char[] master = passwordField.getPassword();
                        DesEncrypter desEncrypter = new DesEncrypter(master);
                        String encrypted$ = desEncrypter.encrypt(textArea.getText());
                        textArea.setText(encrypted$);
                    }
                });

                menu.add(encryptItem);
                JMenuItem decryptItem = new JMenuItem("Decrypt");
                decryptItem.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        char[] master = passwordField.getPassword();
                        DesEncrypter desEncrypter = new DesEncrypter(master);
                        String decrypted$ = desEncrypter.decrypt(textArea.getText());
                        if (decrypted$ != null && decrypted$.length() > 0)
                            textArea.setText(decrypted$);
                    }
                });
                menu.add(decryptItem);
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });
    return menu;
}