Java Swing Menu createMenu(String text)

Here you can find the source of createMenu(String text)

Description

create Menu

License

Open Source License

Declaration

public static JMenu createMenu(String text) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.j  av a2s . co  m*/
 * 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.JMenu;

public class Main {
    public static JMenu createMenu(String text) {
        JMenu menu = new JMenu();
        configureTextAndMnemonic(menu, text);
        return menu;
    }

    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. createMenu(final String text)
  2. createMenu(final String text, final char mnemonic)
  3. createMenu(String label, int mnemonic, String description)
  4. createMenu(String label, Object... objects)
  5. createMenu(String name, Object[] item, int[] accelerator, ActionListener listener)
  6. createMenuKeyMask(final int aKeyStroke, final int... aMasks)
  7. enableMenu(JTextComponent text)
  8. findMenuBar(Container container)
  9. findMenuElement(final MenuElement root, final String text)