Java Swing Menu createMenu(String name, Object[] item, int[] accelerator, ActionListener listener)

Here you can find the source of createMenu(String name, Object[] item, int[] accelerator, ActionListener listener)

Description

create Menu

License

BEER-WARE LICENSE

Declaration

public static JMenu createMenu(String name, Object[] item, int[] accelerator, ActionListener listener) 

Method Source Code


//package com.java2s;
/*/*  ww w. j ava 2  s. c o m*/
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * Dualkarnevalen wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy us a beer in return 
 * ----------------------------------------------------------------------------
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main {
    public static JMenu createMenu(String name, String prefix, Object[] item, int[] accelerator,
            ActionListener listener) {
        JMenu theMenu;
        int itemCounter;
        JMenuItem theItem;

        theMenu = new JMenu(name);
        if (item != null)
            for (itemCounter = 0; itemCounter < item.length; itemCounter++) {
                if (item[itemCounter] == null)
                    theMenu.addSeparator();
                else {
                    if (item[itemCounter] instanceof JMenuItem)
                        theItem = (JMenuItem) item[itemCounter];
                    else if (item[itemCounter] instanceof String) {
                        theItem = new JMenuItem((String) item[itemCounter]);
                        theItem.setActionCommand(prefix + item[itemCounter]);
                    } else
                        continue;

                    if (listener != null)
                        theItem.addActionListener(listener);

                    if (accelerator.length > itemCounter && accelerator[itemCounter] != 0)
                        theItem.setAccelerator(KeyStroke.getKeyStroke(accelerator[itemCounter],
                                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

                    theMenu.add(theItem);
                }
            }

        return theMenu;
    }

    public static JMenu createMenu(String name, Object[] item, int[] accelerator, ActionListener listener) {
        return createMenu(name, "", item, accelerator, listener);
    }
}

Related

  1. createMenu(final String name, final int mnemonic)
  2. createMenu(final String text)
  3. createMenu(final String text, final char mnemonic)
  4. createMenu(String label, int mnemonic, String description)
  5. createMenu(String label, Object... objects)
  6. createMenu(String text)
  7. createMenuKeyMask(final int aKeyStroke, final int... aMasks)
  8. enableMenu(JTextComponent text)
  9. findMenuBar(Container container)