Example usage for javax.swing JMenu getText

List of usage examples for javax.swing JMenu getText

Introduction

In this page you can find the example usage for javax.swing JMenu getText.

Prototype

public String getText() 

Source Link

Document

Returns the button's text.

Usage

From source file:MenuTest.java

public MenuTest() {
    super();//www  . j ava2  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.quattroresearch.antibody.plugin.PluginLoader.java

/**
 * Loads the menu plugins and inserts them into the menu.
 */// www  . java  2 s  .co  m
public void loadMenuPlugins() {
    String menuPluginConfig = PreferencesService.getInstance().getApplicationPrefs().getString("plugins.menu");
    if (menuPluginConfig == null) {
        return;
    }
    String[] menuPlugins = menuPluginConfig.split(";");
    for (String singleConfig : menuPlugins) {
        try {

            Class<?> clazz = classLoader.findClass(singleConfig);

            if (clazz != null) {
                Constructor<?> constructor;

                constructor = clazz.getDeclaredConstructor(JFrame.class);

                AbstractEditorAction action = (AbstractEditorAction) constructor
                        .newInstance(UIService.getInstance().getMainFrame());

                boolean isAdded = false;
                JMenuBar menubar = UIService.getInstance().getMainFrame().getJMenuBar();
                for (int i = 0; i < menubar.getMenuCount(); i++) {
                    JMenu menu = menubar.getMenu(i);
                    if (menu.getText().equals(action.getMenuName())) {
                        menu.add(action);
                        isAdded = true;
                        break;
                    }
                }
                if (!isAdded) {
                    JMenu newMenu = new JMenu(action.getMenuName());
                    newMenu.add(action);
                    menubar.add(newMenu, menubar.getMenuCount() - 1);
                }
            }
        } catch (ClassNotFoundException exc) {
            System.err.println("Menu Plugin class was not found: " + exc.toString());
        } catch (Exception exc) {
            System.err.println(exc.toString());
        }
    }
}

From source file:com.sshtools.common.ui.SshToolsApplicationPanel.java

public void addFavorite(StandardAction action) {
    for (int i = 0; i < menuBar.getMenuCount(); i++) {
        JMenu menu = menuBar.getMenu(i);
        if (menu.getText() != null && menu.getText().equals("Favorites")) {
            menu.add(action);//from   w ww.  ja v  a2s.c o m
        }
    }
}

From source file:ca.phon.app.project.ProjectWindow.java

@Override
public void setJMenuBar(JMenuBar menu) {
    super.setJMenuBar(menu);

    JMenu projectMenu = new JMenu("Project");

    int projectMenuIndex = -1;
    // get the edit menu and add view commands
    for (int i = 0; i < menu.getMenuCount(); i++) {
        JMenu currentBar = menu.getMenu(i);

        if (currentBar != null && currentBar.getText() != null && currentBar.getText().equals("Workspace")) {
            projectMenuIndex = i + 1;//w  w w.  j  ava  2  s  .  c om
        }
    }

    if (projectMenuIndex > 0) {
        menu.add(projectMenu, projectMenuIndex);
    }

    // refresh lists 
    final RefreshAction refreshItem = new RefreshAction(this);
    projectMenu.add(refreshItem);
    projectMenu.addSeparator();

    // create corpus item
    final NewCorpusAction newCorpusItem = new NewCorpusAction(this);
    projectMenu.add(newCorpusItem);

    //       create corpus item
    final NewSessionAction newSessionItem = new NewSessionAction(this);
    projectMenu.add(newSessionItem);

    projectMenu.addSeparator();

    final AnonymizeAction anonymizeParticipantInfoItem = new AnonymizeAction(this);
    projectMenu.add(anonymizeParticipantInfoItem);

    final CheckTranscriptionsAction repairItem = new CheckTranscriptionsAction(this);
    projectMenu.add(repairItem);

    // merge/split sessions
    final DeriveSessionAction deriveItem = new DeriveSessionAction(this);
    projectMenu.add(deriveItem);

    final JMenu teamMenu = new JMenu("Team");
    teamMenu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(MenuEvent e) {
            teamMenu.removeAll();
            if (getProject() != null) {
                final ProjectGitController gitController = new ProjectGitController(getProject());
                if (gitController.hasGitFolder()) {
                    teamMenu.add(new CommitAction(ProjectWindow.this));

                    teamMenu.addSeparator();

                    teamMenu.add(new PullAction(ProjectWindow.this));
                    teamMenu.add(new PushAction(ProjectWindow.this));

                } else {
                    final InitAction initRepoAct = new InitAction(ProjectWindow.this);
                    teamMenu.add(initRepoAct);
                }
            }
        }

        @Override
        public void menuDeselected(MenuEvent e) {

        }

        @Override
        public void menuCanceled(MenuEvent e) {

        }
    });
    projectMenu.addSeparator();
    projectMenu.add(teamMenu);
}

From source file:com.pironet.tda.TDA.java

/**
 * check file menu//ww  w .  ja  v a  2s .co  m
 */
public void menuSelected(MenuEvent e) {
    JMenu source = (JMenu) e.getSource();
    if ((source != null) && "File".equals(source.getText())) {
        // close menu item only active, if something is selected.
        getMainMenu().getCloseMenuItem().setEnabled(tree.getSelectionPath() != null);
        getMainMenu().getCloseToolBarButton().setEnabled(tree.getSelectionPath() != null);
    }
}

From source file:org.jajuk.ui.widgets.JajukJMenuBar.java

/**
 * Apply all mnemonics for all menus (follow i18n).
 *//*from w w w  .jav a 2 s. c o  m*/
private void applyMnemonics() {
    for (int i = 0; i < mainmenu.getMenuCount(); i++) {
        JMenu menu = mainmenu.getMenu(i);
        if (menu != null && StringUtils.isNotBlank(menu.getText())) {
            String label = menu.getText();
            int mnemonic = label.getBytes()[0];
            menu.setMnemonic(mnemonic);
        }
    }
}

From source file:org.kepler.gui.MenuMapper.java

public void init() {

    if (_tableauFrameInstance == null) {
        log.error(//  www  . j a  v a  2s .  c  o  m
                "MenuMapper cannot proceed, due to one or more NULL values:" + "\nptiiMenubar = " + _ptiiMenubar
                        + "\ntableauFrameInstance = " + _tableauFrameInstance + "\ndefaulting to PTII menus");

        return;
    }

    // First, we need to make sure PTII has finished constructing its menus.
    // Those menus are created and assembled in a thread that is started in
    // the
    // pack() method of ptolemy.gui.Top. As that thread finishes, it adds
    // the
    // new ptii JMenuBar to the frame - so we test for that here, and don't
    // proceed until the frame has the new JMenuBar added
    // For safety, so we don't have an infinite loop, we also set a safety
    // counter:
    // maxWaitMS is the longest the app waits for the ptii
    // menus to be added, before it continues anyway.
    final int maxWaitMS = 500;
    // sleepTimeMS is the amount of time it sleeps per loop:
    final int sleepTimeMS = 5;
    // ////
    final int maxLoops = maxWaitMS / sleepTimeMS;
    int safetyCounter = 0;

    while (safetyCounter++ < maxLoops && !_tableauFrameInstance.isMenuPopulated()) {

        if (isDebugging) {
            log.debug("Waiting for PTII menus to be created... " + safetyCounter);
        }
        try {
            Thread.sleep(sleepTimeMS);
        } catch (Exception e) {
            // ignore
        }
    }

    JMenuBar _keplerMenubar = null;
    if (_ptiiMenubar == null) {
        _ptiiMenubar = _tableauFrameInstance.getJMenuBar();
    }

    if (_ptiiMenubar != null) {

        // gets here if a PTII menubar has been added to frame...

        // 1) Now PTII has finished constructing its menus, get all
        // menu items and put them in a Map for easier access later...
        _ptiiMenuActionsMap = getPTIIMenuActionsMap();
        Map<String, Action> ptiiMenuActionsMap = _ptiiMenuActionsMap;

        // 2) Now we have all the PTII menu items, get the
        // Kepler-specific menu mappings from the preferences file,
        // then go thru the Kepler menu mappings and
        // populate the new menubar with Kepler menus,
        // creating any new menu items that don't exist yet

        // this is a Map that will be used to keep track of
        // what we have added to the menus, and in what order
        _keplerMenubar = createKeplerMenuBar(ptiiMenuActionsMap);

        if (_keplerMenubar != null) {

            // First, look to see if any menus are empty. If
            // they are, remove the top-level menu from the menubar...
            // ** NOTE - do these by counting *down* to zero, otherwise the
            // menus'
            // indices change dynamically as we remove menus, causing
            // errors!
            for (int m = _keplerMenubar.getMenuCount() - 1; m >= 0; m--) {
                JMenu nextMenu = _keplerMenubar.getMenu(m);
                if (nextMenu.getMenuComponentCount() < 1) {
                    if (isDebugging) {
                        log.debug("deleting empty menu: " + nextMenu.getText());
                    }
                    _keplerMenubar.remove(nextMenu);
                }
            }
            // hide the ptii menubar
            _tableauFrameInstance.hideMenuBar();

            // add the new menu bar
            _tableauFrameInstance.setJMenuBar(_keplerMenubar);
        } else {
            log.error("Problem creating Kepler menus - defaulting to PTII menus");
        }

    } else {
        // gets here if a PTII menubar has *NOT* been added to frame...
        // Therefore, this frame doesn't have a menubar by default,
        // so we probably shouldn't add one, for now, at least

        // hide the ptii menubar (may not be necessary)
        _tableauFrameInstance.hideMenuBar();

        // add the new menu bar (may not be necessary)
        _tableauFrameInstance.setJMenuBar(null);
    }
}

From source file:org.kepler.gui.MenuMapper.java

public Map<String, Action> getPTIIMenuActionsMap() {

    if (_ptiiMenubar == null) {
        _ptiiMenubar = _tableauFrameInstance.getJMenuBar();
    }/*from  w  w  w  . j  av a 2  s .  com*/

    if (_ptiiMenuActionsMap == null || _reloadPtolemyMenus) {
        if (isDebugging) {
            log.debug("**************\nEXISTING PTII MENUS:\n**************\n");
        }

        // NOTE: use a LinkedHashMap to preserve the order of the
        // recently opened files list
        _ptiiMenuActionsMap = new LinkedHashMap<String, Action>();

        for (int m = 0; m < _ptiiMenubar.getMenuCount(); m++) {
            JMenu nextMenu = _ptiiMenubar.getMenu(m);

            storePTIITopLevelMenus(nextMenu, nextMenu.getText().toUpperCase(), MENU_PATH_DELIMITER,
                    _ptiiMenuActionsMap);
        }
        _reloadPtolemyMenus = false;
        if (isDebugging) {
            log.debug("\n**************\nEND PTII MENUS:\n*****************\n");
        }
    }
    return _ptiiMenuActionsMap;
}

From source file:org.kepler.gui.MenuMapper.java

public static JMenuItem addMenuFor(String key, Action action, JComponent topLvlContainer,
        Map<String, JMenuItem> keplerMenuMap) {

    if (topLvlContainer == null) {
        if (isDebugging) {
            log.debug("NULL container received (eg JMenuBar) - returning NULL");
        }//from  w w w  . j  a  v  a2 s . c  o  m
        return null;
    }
    if (key == null) {
        if (isDebugging) {
            log.debug("NULL key received");
        }
        return null;
    }
    key = key.trim();

    if (key.length() < 1) {
        if (isDebugging) {
            log.debug("BLANK key received");
        }
        return null;
    }
    if (action == null && key.indexOf(MENU_SEPARATOR_KEY) < 0) {
        if (isDebugging) {
            log.debug("NULL action received, but was not a separator: " + key);
        }
        return null;
    }

    if (keplerMenuMap.containsKey(key)) {
        if (isDebugging) {
            log.debug("Menu already added; skipping: " + key);
        }
        return null;
    }

    // split delimited parts and ensure menus all exist
    String[] menuLevel = key.split(MENU_PATH_DELIMITER);

    int totLevels = menuLevel.length;

    // create a menu for each "menuLevel" if it doesn't already exist
    final StringBuffer nextLevelBuff = new StringBuffer();
    String prevLevelStr = null;
    JMenuItem leafMenuItem = null;

    for (int levelIdx = 0; levelIdx < totLevels; levelIdx++) {

        // save previous value
        prevLevelStr = nextLevelBuff.toString();

        String nextLevelStr = menuLevel[levelIdx];
        // get the index of the first MNEMONIC_SYMBOL
        int mnemonicIdx = nextLevelStr.indexOf(MNEMONIC_SYMBOL);
        char mnemonicChar = 0;

        // if an MNEMONIC_SYMBOL exists, remove all underscores. Then, idx
        // of
        // first underscore becomes idx of letter it used to precede - this
        // is the mnemonic letter
        if (mnemonicIdx > -1) {
            nextLevelStr = nextLevelStr.replaceAll(MNEMONIC_SYMBOL, "");
            mnemonicChar = nextLevelStr.charAt(mnemonicIdx);
        }
        if (levelIdx != 0) {
            nextLevelBuff.append(MENU_PATH_DELIMITER);
        }
        nextLevelBuff.append(nextLevelStr);

        // don't add multiple separators together...
        if (nextLevelStr.indexOf(MENU_SEPARATOR_KEY) > -1) {
            if (separatorJustAdded == false) {

                // Check if we're at the top level, since this makes sense
                // only for
                // context menu - we can't add a separator to a JMenuBar
                if (levelIdx == 0) {
                    if (topLvlContainer instanceof JContextMenu) {
                        ((JContextMenu) topLvlContainer).addSeparator();
                    }
                } else {
                    JMenu parent = (JMenu) keplerMenuMap.get(prevLevelStr);

                    if (parent != null) {
                        if (parent.getMenuComponentCount() < 1) {
                            if (isDebugging) {
                                log.debug("------ NOT adding separator to parent " + parent.getText()
                                        + ", since it does not contain any menu items");
                            }
                        } else {
                            if (isDebugging) {
                                log.debug("------ adding separator to parent " + parent.getText());
                            }
                            // add separator to parent
                            parent.addSeparator();
                            separatorJustAdded = true;
                        }
                    }
                }
            }
        } else if (!keplerMenuMap.containsKey(nextLevelBuff.toString())) {
            // If menu has not already been created, we need
            // to create it and then add it to the parent level...

            JMenuItem menuItem = null;

            // if we're at a "leaf node" - need to create a JMenuItem
            if (levelIdx == totLevels - 1) {

                // save old display name to use as actionCommand on
                // menuitem,
                // since some parts of PTII still
                // use "if (actionCommand.equals("SaveAs")) {..." etc
                String oldDisplayName = (String) action.getValue(Action.NAME);

                // action.putValue(Action.NAME, nextLevelStr);

                if (mnemonicChar > 0) {
                    action.putValue(GUIUtilities.MNEMONIC_KEY, new Integer(mnemonicChar));
                }

                // Now we look to see if it's a checkbox
                // menu item, or just a regular one
                String menuItemType = (String) (action.getValue(MENUITEM_TYPE));

                if (menuItemType != null && menuItemType == CHECKBOX_MENUITEM_TYPE) {
                    menuItem = new JCheckBoxMenuItem(action);
                } else {
                    menuItem = new JMenuItem(action);
                }

                // --------------------------------------------------------------
                /** @todo - setting menu names - TEMPORARY FIX - FIXME */
                // Currently, if we use the "proper" way of setting menu
                // names -
                // ie by using action.putValue(Action.NAME, "somename");,
                // then
                // the name appears on the port buttons on the toolbar,
                // making
                // them huge. As a temporary stop-gap, I am just setting the
                // new
                // display name using setText() instead of
                // action.putValue(..,
                // but this needs to be fixed elsewhere - we want to be able
                // to
                // use action.putValue(Action.NAME (ie uncomment the line
                // above
                // that reads:
                // action.putValue(Action.NAME, nextLevelStr);
                // and delete the line below that reads:
                // menuItem.setText(nextLevelStr);
                // otherwise this may bite us in future...
                menuItem.setText(nextLevelStr);
                // --------------------------------------------------------------

                // set old display name as actionCommand on
                // menuitem, for ptii backward-compatibility
                menuItem.setActionCommand(oldDisplayName);

                // add JMenuItem to the Action, so it can be accessed by
                // Action code
                action.putValue(NEW_JMENUITEM_KEY, menuItem);
                leafMenuItem = menuItem;
            } else {
                // if we're *not* at a "leaf node" - need to create a JMenu
                menuItem = new JMenu(nextLevelStr);
                if (mnemonicChar > 0) {
                    menuItem.setMnemonic(mnemonicChar);
                }
            }
            // level 0 is a special case, since the container (JMenuBar or
            // JContextMenu) is not a JMenu or a JMenuItem, so we can't
            // use the same code to add child to parent...
            if (levelIdx == 0) {
                if (topLvlContainer instanceof JMenuBar) {
                    // this handles JMenuBar menus
                    ((JMenuBar) topLvlContainer).add(menuItem);
                } else if (topLvlContainer instanceof JContextMenu) {
                    // this handles popup context menus
                    ((JContextMenu) topLvlContainer).add(menuItem);
                }
                // add to Map
                keplerMenuMap.put(nextLevelBuff.toString(), menuItem);
                separatorJustAdded = false;
            } else {
                JMenu parent = (JMenu) keplerMenuMap.get(prevLevelStr);
                if (parent != null) {
                    // add to parent
                    parent.add(menuItem);
                    // add to Map
                    keplerMenuMap.put(nextLevelBuff.toString(), menuItem);
                    separatorJustAdded = false;
                } else {
                    if (isDebugging) {
                        log.debug("Parent menu is NULL" + prevLevelStr);
                    }
                }
            }
        }
    }
    return leafMenuItem;
}

From source file:org.openmicroscopy.shoola.env.ui.TaskBarView.java

/**
 * Copies the items from the specified menu and creates a new menu.
 *
 * @param original The menu to handle./*from  w w  w  . ja v a  2  s.  c om*/
 * @return See above.
 */
private JMenu copyItemsFromMenu(JMenu original) {
    Component[] comps = original.getPopupMenu().getComponents();
    JMenu menu = new JMenu();
    menu.setText(original.getText());
    menu.setToolTipText(original.getToolTipText());
    ActionListener[] al = original.getActionListeners();
    for (int j = 0; j < al.length; j++)
        menu.addActionListener(al[j]);
    MenuKeyListener[] mkl = original.getMenuKeyListeners();
    for (int j = 0; j < mkl.length; j++)
        menu.addMenuKeyListener(mkl[j]);
    MenuListener[] ml = original.getMenuListeners();
    for (int j = 0; j < ml.length; j++)
        menu.addMenuListener(ml[j]);
    for (int i = 0; i < comps.length; i++) {
        if (comps[i] instanceof JMenu) {
            menu.add(copyItemsFromMenu((JMenu) comps[i]));
        } else if (comps[i] instanceof JMenuItem) {
            menu.add(copyItem((JMenuItem) comps[i]));
        } else if (comps[i] instanceof JSeparator) {
            menu.add(new JSeparator(JSeparator.HORIZONTAL));
        }
    }
    return menu;
}