Java Code Examples for javax.swing.JMenuBar#getMenuCount()

The following examples show how to use javax.swing.JMenuBar#getMenuCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowMenu.java    From pumpernickel with MIT License 6 votes vote down vote up
/**
 * On Mac often the menus won't really update without this hack-ish twist:
 * remove the menu and re-add it. Voila! It's both unnecessary and crucial
 * at the same time.
 * 
 * @param f
 *            the frame whose menubar you need to update.
 * @param menu
 *            the menu you need to update.
 */
static void fixMenuBar(JFrame f, JMenu menu) {
	JMenuBar mb = f.getJMenuBar();
	if (mb != null) {
		JMenu[] menus = new JMenu[mb.getMenuCount()];
		for (int a = 0; a < menus.length; a++) {
			menus[a] = mb.getMenu(a);
		}

		boolean found = false;
		List<JMenu> menusToAdd = new ArrayList<>();
		for (int a = 0; a < menus.length; a++) {
			if (menus[a] == menu)
				found = true;

			if (found) {
				mb.remove(menus[a]);
				menusToAdd.add(menus[a]);
			}
		}
		for (JMenu menuToAdd : menusToAdd) {
			mb.add(menuToAdd);
		}
	}
}
 
Example 2
Source File: MainFrame.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void addNotify() {
    super.addNotify();

    float size = Driver.getFontSize();

    JMenuBar menubar = getJMenuBar();
    if (menubar != null) {
        menubar.setFont(menubar.getFont().deriveFont(size));
        for (int i = 0; i < menubar.getMenuCount(); i++) {
            for (int j = 0; j < menubar.getMenu(i).getMenuComponentCount(); j++) {
                Component temp = menubar.getMenu(i).getMenuComponent(j);
                temp.setFont(temp.getFont().deriveFont(size));
            }
        }
        mainFrameTree.updateFonts(size);
    }
}
 
Example 3
Source File: GUIUtils.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Enables and disables the menu bars in DrawingFrames and DrawingFrame3D.
 *
 * Usually invoked when a model is initialized but may be invoked at other times.
 */
public static void enableMenubars(boolean enable) {
  Frame[] frames = Frame.getFrames();
  for(int i = 0; i<frames.length; i++) {
    if(!frames[i].isDisplayable()) {
      continue;
    }
    if((frames[i].getName()!=null)&&(frames[i].getName().indexOf("Tool")>-1)) {       //$NON-NLS-1$
      continue;
    }
    Class<?> frame3d = null;
    try {
      frame3d = Class.forName("org.opensourcephysics.display3d.core.DrawingFrame3D"); //$NON-NLS-1$
    } catch(ClassNotFoundException ex) {}
    if(DrawingFrame.class.isInstance(frames[i])||((frame3d!=null)&&frame3d.isInstance(frames[i]))) {
      JMenuBar bar = ((JFrame) frames[i]).getJMenuBar();
      if(bar!=null) {
        for(int j = 0, n = bar.getMenuCount(); j<n; j++) {
          bar.getMenu(j).setEnabled(enable);
        }
      }
    }
  }
}
 
Example 4
Source File: OSPFrame.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets a menu with the given name from the menu bar.  Returns null if menu item does not exist.
 *
 * @param menuName String
 * @return JMenu
 */
public JMenu getMenu(String menuName) {
  JMenuBar menuBar = getJMenuBar();
  if(menuBar==null) {
    return null;
  }
  menuName = menuName.trim();
  JMenu menu = null;
  for(int i = 0; i<menuBar.getMenuCount(); i++) {
    JMenu next = menuBar.getMenu(i);
    if(next.getText().trim().equals(menuName)) {
      menu = next;
      break;
    }
  }
  return menu;
}
 
Example 5
Source File: OSPFrame.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Removes a menu with the given name from the menu bar and returns the removed item.
 * Returns null if menu item does not exist.
 *
 * @param menuName String
 * @return JMenu
 */
public JMenu removeMenu(String menuName) {
  JMenuBar menuBar = getJMenuBar();
  if(menuBar==null) {
    return null;
  }
  menuName = menuName.trim();
  JMenu menu = null;
  for(int i = 0; i<menuBar.getMenuCount(); i++) {
    JMenu next = menuBar.getMenu(i);
    if(next.getText().trim().equals(menuName)) {
      menu = next;
      menuBar.remove(i);
      break;
    }
  }
  return menu;
}
 
Example 6
Source File: WindowActionManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void update() {
	JMenuBar menuBar = menuBarMgr.getMenuBar();
	if (menuBar.getMenuCount() > 0) {
		node.setMenuBar(menuBar);
	}

	node.setToolBar(toolBarMgr.getToolBar());
	node.validate();
}
 
Example 7
Source File: MnemonicFactory.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public static void setMnemonics(final JMenuBar menuBar) {
    final int c = menuBar.getMenuCount();
    final ArrayList<JMenuItem> list = new ArrayList<JMenuItem>(c);
    for (int i = 0; i < c; i++)
        list.add(menuBar.getMenu(i));
    setMnemonics(list);
}
 
Example 8
Source File: UIUtils.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public static int findMenuPosition(JMenuBar menuBar, String name) {
    int n = menuBar.getMenuCount();
    for (int i = 0; i < n; i++) {
        JMenu menu = menuBar.getMenu(i);
        if (name.equals(menu.getName())) {
            return i;
        }
    }
    return -1;
}