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

booleanisMenuCrossMenuMnemonics()
is Menu Cross Menu Mnemonics
return UIManager.getBoolean("Menu.crossMenuMnemonic");
voidlimitMenuSize(List items, String name, int size)
This takes the list of JMenuItems and, for each JMenu, will ensure that there are no more than size number of items in any sub menu.
for (int i = 0; i < items.size(); i++) {
    Object o = items.get(i);
    if (!(o instanceof JMenu)) {
        continue;
    JMenu menu = (JMenu) o;
    limitMenuSize(menu, name, size);
JMenuBarmakeMenuBar(List menus)
Create a JMenuBar and add the menus contained with the menus list If no menus then return null.
if ((menus == null) || (menus.size() == 0)) {
    return null;
JMenuBar menuBar = new JMenuBar();
for (int i = 0; i < menus.size(); i++) {
    menuBar.add((JMenu) menus.get(i));
return menuBar;
...
voidmakeMenubarNice()
Optimizes the appeareance of the JMenubar.
UIManager.put("MenuBar.border", getEtchedBottomBorder(0, 2));
KeyStrokemenuDigit(int digit)
menu Digit
if (digit < 0 || digit > 9)
    throw new IllegalArgumentException("Not a digit: " + digit);
return menuKeystroke(KeyEvent.VK_0 + digit);
KeyStrokemenuKeystroke(int virtualKey)
menu Keystroke
return KeyStroke.getKeyStroke(virtualKey, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
voidremoveMenuMnemonics(MenuElement[] elements)
Remove the menu shortcut Mnemonics from the given MenuElement and its sub MenuElements.
for (int i = 0; i < elements.length; i++) {
    MenuElement element = elements[i];
    if (element instanceof JMenuItem) {
        JMenuItem item = (JMenuItem) element;
        item.setMnemonic(-1);
    removeMenuMnemonics(element.getSubElements());
voidsetFontsToMenu(MenuElement menuElement, Font font)
set Fonts To Menu
MenuElement[] subElements = menuElement.getSubElements();
menuElement.getComponent().setFont(font);
menuElement.getComponent().invalidate();
if (subElements == null)
    return;
for (int i = 0; i < subElements.length; i++)
    setFontsToMenu(subElements[i], font);
voidsetMenuFont(Object item, Font theFont)
set Menu Font
if (item instanceof JMenu) {
    JMenu menu = (JMenu) item;
    menu.setFont(theFont);
    for (int j = 0; j < menu.getItemCount(); j++) {
        if (menu.getItem(j) != null) {
            setMenuFont(menu.getItem(j), theFont);
} else {
    JMenuItem menuItem = (JMenuItem) item;
    menuItem.setFont(theFont);
    for (int i = 0; i < menuItem.getSubElements().length; i++) {
        setMenuFont((JMenuItem) menuItem.getSubElements()[i], theFont);
voidsetMenuText(AbstractButton item, String text, boolean useMnemonic)
DOCUMENT ME!
int i = text.indexOf('&');
if (i < 0) {
    item.setText(text);
} else {
    item.setText(text.substring(0, i) + text.substring(i + 1));
    if (useMnemonic) {
        item.setMnemonic(text.charAt(i + 1));