Java Utililty Methods JMenu

List of utility methods to do JMenu

Description

The list of methods to do JMenu are organized into topic(s).

Method

JMenuItemcreateMenuItemCustomAction(JMenu menu, int type, String text, String command, ImageIcon image, int acceleratorKey, String toolTip, ActionListener action)
create Menu Item Custom Action
JMenuItem item;
switch (type) {
case RADIO:
    item = new JRadioButtonMenuItem();
    break;
case CHECK:
    item = new JCheckBoxMenuItem();
    break;
...
voidcreateMenuItems(JMenu menu, String[] menuItemNames)
create Menu Items
ArrayList menuItems = new ArrayList();
JMenuItem menuItem = null;
for (int i = 0; i < menuItemNames.length; i++) {
    menuItem = new JMenuItem(menuItemNames[i]);
    menuItem.setMnemonic(menuItemNames[i].charAt(0));
    menu.add(menuItem);
JMenucreateTooltiplessJMenu(Action action)
create Tooltipless J Menu
JMenu result = new JMenu(action) {
    @Override
    public JMenuItem add(Action a) {
        JMenuItem result = super.add(a);
        result.setToolTipText(null);
        return result;
};
...
voiddisableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)
disables (recursively) the given menu if all menu items in the menu (all direct children) are disabled.
int iEnabledMenuItems = 0;
for (Component comp : menu.getMenuComponents()) {
    if (comp instanceof JMenuItem) {
        final JMenuItem mi = (JMenuItem) comp;
        if (mi instanceof JMenu) {
            final JMenu menuChild = (JMenu) mi;
            disableMenuIfAllMenuItemsAreDisabled(menuChild, bRemoveEmptySubMenus);
            if (bRemoveEmptySubMenus && !mi.isEnabled()) {
...
voidenableAll(JMenu menu)
Convenience method that recursively enables every JMenu and JMenuItem inside "menu".
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
    Component obj = menu.getMenuComponent(i);
    if (obj instanceof JMenuItem) {
        ((JMenuItem) obj).setEnabled(true);
    } else if (obj instanceof JMenu) {
        enableAll((JMenu) obj);
voidfillFontMenu(JMenu fontMenu)
fill Font Menu
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
List<String> names = new ArrayList<>();
for (Font f : fonts) {
    String name = f.getFamily();
    if (!names.contains(name)) {
        addMenuItem(null, fontMenu, name);
        names.add(name);
...
TfindMenuComponent(JMenu menu, String menuComponentName, Class componentClass)
find Menu Component
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
    Component component = menu.getMenuComponent(i);
    if (menuComponentName.equals(component.getName()) && componentClass.isInstance(component))
        return componentClass.cast(component);
return null;
JMenuItemfindMenuItem(JMenu menu, String menuItemName)
find Menu Item
if (menu.getItemCount() > 0) {
    for (int ii = 0; ii < menu.getItemCount(); ++ii) {
        JMenuItem menuItem = menu.getItem(ii);
        if (menuItem.getText().equals(menuItemName))
            return menuItem;
return null;
...
JMenuItemfindMenuItemByActionCommand(JMenu menu, String sActionCommand)
find Menu Item By Action Command
for (Component comp : menu.getMenuComponents()) {
    if (comp instanceof JMenuItem) {
        final JMenuItem mi = (JMenuItem) comp;
        if (sActionCommand.equals(mi.getActionCommand())) {
            return mi;
return null;
JMenufindSubMenuWithLabel(JMenu menu, String text)
find Sub Menu With Label
for (Component submenu : menu.getMenuComponents()) {
    if (submenu instanceof JMenu && text.equals(((JMenu) submenu).getText())) {
        return (JMenu) submenu;
return null;