Example usage for javax.swing JMenuBar add

List of usage examples for javax.swing JMenuBar add

Introduction

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

Prototype

public Component add(String name, Component comp) 

Source Link

Document

Adds the specified component to this container.

Usage

From source file:com.net2plan.gui.GUINet2Plan.java

private static JMenuItem getCurrentMenu(JMenuBar menubar, JMenu parent, String itemName) {
    int pos = itemName.indexOf('|');
    if (pos == -1) {
        if (parent == null)
            throw new RuntimeException("Bad");

        JMenuItem menuItem = new JMenuItem(itemName);
        parent.add(menuItem);//  ww  w  . j a  v  a2 s .c  o m

        return menuItem;
    } else {
        String parentName = itemName.substring(0, pos);
        JMenu new_parent = null;

        MenuElement[] children;
        if (menubar != null)
            children = menubar.getSubElements();
        else if (parent != null)
            children = parent.getSubElements();
        else
            throw new RuntimeException("Bad");

        for (MenuElement item : children) {
            if (!(item instanceof JMenu) || !((JMenu) item).getText().equalsIgnoreCase(parentName))
                continue;

            new_parent = (JMenu) item;
            break;
        }

        if (new_parent == null) {
            new_parent = new JMenu(parentName);

            if (menubar != null) {
                if (parentName.equals("Tools")) {
                    menubar.add(new_parent, 1);
                    new_parent.setMnemonic('T');
                } else {
                    menubar.add(new_parent, menubar.getComponentCount() - 1);
                }
            } else if (parent != null) {
                parent.add(new_parent);
            } else {
                throw new RuntimeException("Bad");
            }
        }

        String new_itemName = itemName.substring(pos + 1);

        return getCurrentMenu(null, new_parent, new_itemName);
    }
}

From source file:com.quattroresearch.antibody.plugin.PluginLoader.java

/**
 * Loads the menu plugins and inserts them into the menu.
 *//*from ww  w  .  j  a v a2 s.com*/
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: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 .co  m
        }
    }

    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:org.notebook.gui.widget.LookAndFeelSelector.java

@EventAction(order = 1)
public void GuiInited(BroadCastEvent event) {
    Object o = event.getSource();
    if (o instanceof JFrame) {
        frame = (JFrame) o;/*from   w w  w  . j  a  va2s. com*/
        JMenuBar mb = frame.getJMenuBar();
        mb.add(createMenu(), 2);
        mb.validate();
    }
}

From source file:org.openmicroscopy.shoola.agents.imviewer.view.ImViewerUI.java

/** 
 * Sets the selected pane.//from  w w w . j a v  a2s  .  c  o m
 * 
 * @param index The index of the selected tab pane.
 */
void setSelectedPane(int index) {
    JMenuBar menuBar = getJMenuBar();
    Component[] items = menuBar.getComponents();
    Component item;
    int j = -1;
    for (int i = 0; i < items.length; i++) {
        item = items[i];
        if (item == zoomGridMenu || item == zoomMenu)
            j = i;
    }
    if (j != -1)
        menuBar.remove(j);
    double f;
    switch (index) {
    case ImViewer.GRID_INDEX:
        if (j != -1)
            menuBar.add(zoomGridMenu, j);
        f = model.getBrowser().getGridRatio();
        setMagnificationStatus(f, ZoomAction.getIndex(f));
        break;
    case ImViewer.PROJECTION_INDEX:
    case ImViewer.VIEW_INDEX:
    default:
        if (j != -1)
            menuBar.add(zoomMenu, j);
        f = model.getZoomFactor();
        setMagnificationStatus(f, ZoomAction.getIndex(f));
    }
    int oldIndex = model.getTabbedIndex();
    model.setTabbedIndex(index);
    tabs.removeChangeListener(controller);
    int n = tabs.getTabCount();
    Component c;
    int tabbedIndex;
    for (int i = 0; i < n; i++) {
        c = tabs.getComponentAt(i);
        if (c instanceof ClosableTabbedPaneComponent) {
            tabbedIndex = ((ClosableTabbedPaneComponent) c).getIndex();
            if (tabbedIndex == index)
                tabs.setSelectedIndex(i);
        }
    }

    tabs.addChangeListener(controller);
    setLeftStatus();
    setPlaneInfoStatus();
    model.getBrowser().setSelectedPane(index);
    setLensVisible(isLensVisible(), oldIndex);
    maximizeWindow();
}

From source file:org.parosproxy.paros.extension.ExtensionLoader.java

private void addMenuHelper(JMenuBar menuBar, List<JMenuItem> items, int existingCount) {
    for (JMenuItem item : items) {
        if (item != null) {
            menuBar.add(item, menuBar.getMenuCount() - existingCount);
        }/*from   w  ww  .j ava 2s  .  c o m*/
    }
    menuBar.revalidate();
}