Java Utililty Methods Swing Menu

List of utility methods to do Swing Menu

Description

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

Method

JMenucreateMenu(final String text)
create Menu
return new JMenu(text);
JMenucreateMenu(final String text, final char mnemonic)
create Menu
JMenu menu = new JMenu(text);
menu.setMnemonic(mnemonic);
return menu;
JMenucreateMenu(String label, int mnemonic, String description)
create Menu
return createMenuItem(new JMenu(label), mnemonic, description, null);
JMenucreateMenu(String label, Object... objects)
create Menu
return createMenu(label, -1, objects);
JMenucreateMenu(String name, Object[] item, int[] accelerator, ActionListener listener)
create Menu
return createMenu(name, "", item, accelerator, listener);
JMenucreateMenu(String text)
create Menu
JMenu menu = new JMenu();
configureTextAndMnemonic(menu, text);
return menu;
KeyStrokecreateMenuKeyMask(final int aKeyStroke, final int... aMasks)
Convenience method to create a key mask for menu's.
int modifiers = getMenuShortcutKeyMask();
for (int aMask : aMasks) {
    modifiers |= aMask;
return KeyStroke.getKeyStroke(aKeyStroke, modifiers);
voidenableMenu(JTextComponent text)
enable Menu
final Action actionCopySelected = text.getActionMap().get(DefaultEditorKit.copyAction);
final Action actionSelectAll = text.getActionMap().get(DefaultEditorKit.selectAllAction);
final Action actionUnselect = text.getActionMap().get("unselect");
Action actionCopyAll = new AbstractAction("Copy All to System Clipboard") {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (actionSelectAll != null) {
            actionSelectAll.actionPerformed(e);
...
JMenuBarfindMenuBar(Container container)
Traverse a container hierarchy and returns the first JMenuBar it finds.
Component[] components = container.getComponents();
for (Component component : components) {
    if (component instanceof JMenuBar) {
        return (JMenuBar) component;
    } else if (component instanceof Container) {
        JMenuBar jmb = findMenuBar((Container) component);
        if (jmb != null) {
            return jmb;
...
MenuElementfindMenuElement(final MenuElement root, final String text)
find Menu Element
for (MenuElement item : root.getSubElements()) {
    if (item instanceof JMenu == true) {
        JMenu mItem = (JMenu) item;
        if (mItem.getSubElements().length > 0) {
            MenuElement elem = findMenuElement(mItem, text);
            if (elem != null)
                return elem;
        continue;
    if (item instanceof JPopupMenu == true) {
        JPopupMenu mItem = (JPopupMenu) item;
        if (mItem.getSubElements().length > 0) {
            MenuElement elem = findMenuElement(mItem, text);
            if (elem != null)
                return elem;
        continue;
    if (item instanceof JMenuItem == false)
        continue;
    JMenuItem mItem = (JMenuItem) item;
    if (mItem.getText().equals(text) == true)
        return mItem;
return null;