Java JMenu createMenuItem(JMenu parent, String text)

Here you can find the source of createMenuItem(JMenu parent, String text)

Description

create Menu Item

License

Open Source License

Declaration

public static JMenuItem createMenuItem(JMenu parent, String text) 

Method Source Code


//package com.java2s;
/*//ww w .j  av  a 2s.  c  om
 * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */

import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class Main {
    public static JMenuItem createMenuItem(JMenu parent, String text) {
        JMenuItem mi = new JMenuItem();
        configureTextAndMnemonic(mi, text);
        parent.add(mi);
        return mi;
    }

    public static JMenuItem createMenuItem(JMenu parent, String text, Action action) {
        JMenuItem mi = new JMenuItem(action);
        configureTextAndMnemonic(mi, text);
        parent.add(mi);
        return mi;
    }

    public static void configureTextAndMnemonic(AbstractButton button, String text) {
        int length = text.length();
        int index = text.indexOf('&');
        if (index == -1) {
            button.setText(text);
        } else {
            StringBuilder newText = new StringBuilder(length);
            int mnemonic = -1;
            int mnemonicIndex = -1;
            for (int i = 0; i < length; i++) {
                char aChar = text.charAt(i);
                if (aChar == '\\') {
                    if (i + 1 < length && text.charAt(i + 1) == '&') {
                        i++;
                        newText.append('&');
                    } else {
                        newText.append(aChar);
                    }
                } else if (aChar == '&') {
                    if (i + 1 < length) {
                        if (mnemonic != -1) {
                            throw new IllegalArgumentException("Mnemonic already defined " + text);
                        }
                        aChar = text.charAt(i + 1);
                        if (aChar >= 'a' && aChar <= 'z') {
                            mnemonic = KeyEvent.VK_A + (aChar - 'a');
                        } else if (aChar >= 'A' && aChar <= 'Z') {
                            mnemonic = KeyEvent.VK_A + (aChar - 'A');
                        } else {
                            throw new IllegalArgumentException("Not valid mnemonic " + text);
                        }
                        mnemonicIndex = newText.length();
                    } else {
                        newText.append(aChar);
                    }
                } else {
                    newText.append(aChar);
                }
            }
            button.setText(newText.toString());
            if (mnemonic != -1) {
                button.setMnemonic(mnemonic);
                button.setDisplayedMnemonicIndex(mnemonicIndex);
            }
        }
    }
}

Related

  1. constructRecentlyFilesMenuItems(JMenu recentlyOpenFilesMenuItem, LinkedHashMap filesOpenedLHM)
  2. containsMenuComponent(JMenu parent, Component comp)
  3. countCheckBoxMenuItem(JMenu menu)
  4. createMenu(String menuText, JMenu menu, ActionListener listener)
  5. createMenuBar(JMenu... menues)
  6. createMenuItemCustomAction(JMenu menu, int type, String text, String command, ImageIcon image, int acceleratorKey, String toolTip, ActionListener action)
  7. createMenuItems(JMenu menu, String[] menuItemNames)
  8. createTooltiplessJMenu(Action action)
  9. disableMenuIfAllMenuItemsAreDisabled(JMenu menu, boolean bRemoveEmptySubMenus)