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

voidfixIconTextGap(JComponent menu)
fix Icon Text Gap
if (isNimbusLaF()) {
    Component[] components = menu.getComponents();
    for (Component component : components) {
        if (component instanceof AbstractButton) {
            AbstractButton b = (AbstractButton) component;
            b.setIconTextGap(0);
        if (component instanceof JMenu)
...
CollectiongetAllMenuSubElements(MenuElement root)
Returns all sub menu elements of the specified menu element.
Collection<MenuElement> result = new ArrayList<MenuElement>();
for (MenuElement e : root.getSubElements()) {
    result.add(e);
    result.addAll(getAllMenuSubElements(e));
return result;
intgetMenuFontHeight()
get Menu Font Height
return getFontHeight(MENU_FONT_KEY);
KeyStrokegetMenuKeyStroke(String stroke)
get Menu Key Stroke
KeyStroke key = KeyStroke.getKeyStroke(stroke);
if (key != null && (key.getModifiers() & Event.CTRL_MASK) != 0) {
    key = KeyStroke.getKeyStroke(key.getKeyCode(), (key.getModifiers() & ~(Event.CTRL_MASK + 0x80))
            | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), key.isOnKeyRelease());
return key;
JLabelgetMenuSectionLabel(String string)
get Menu Section Label
JLabel l = new JLabel(string.toUpperCase());
l.setFont(new Font(l.getFont().getFamily(), Font.BOLD, 14));
l.setForeground(new Color(20, 20, 20));
return l;
Object[]getOperationMenu(MenuElement currentMenu, String path)
get Operation Menu
if (path == null || path.equals(""))
    return new Object[] { currentMenu, path };
ArrayList<JMenu> menus = new ArrayList<JMenu>();
if ((Object) currentMenu instanceof JMenuBar) {
    JMenuBar bar = (JMenuBar) currentMenu;
    int count = bar.getMenuCount();
    for (int i = 0; i < count; i++) {
        menus.add(bar.getMenu(i));
...
ListgetParsedElements(MenuElement[] elements)
get Parsed Elements
List<String> returnable = new ArrayList<String>();
for (MenuElement e : elements) {
    if (JMenuItem.class.isAssignableFrom(e.getClass())) {
        returnable.add(((JMenuItem) e).getText());
return returnable;
JMenuBargetTheMenuBar(String[][] arr)
get The Menu Bar
JMenuBar jmb = new JMenuBar();
JMenu jm;
JMenuItem jmi;
for (int i = 0; i < arr.length; i++) {
    jm = new JMenu(arr[i][0]);
    for (int j = 1; j < arr[i].length; j++) {
        jmi = new JMenuItem(arr[i][j]);
        jm.add(jmi);
...
booleanhasIcons(MenuElement menuElement)
Return whether there are any icons at the moment.
MenuElement[] elements = menuElement.getSubElements();
for (int i = 0; i < elements.length; i++) {
    MenuElement element = elements[i];
    if (element instanceof JMenuItem && (((JMenuItem) element).getIcon() != null)) {
        return true;
    if (element instanceof JPopupMenu) {
        return hasIcons(element);
...
voidinstallMenuKeyListener(Component cmp, MenuKeyListener keyListener)
Installs the key listener in the tree of components where the argument component is included, starting in the first parent JFrame or JDialog parent.
List<Component> components = getAllComponents(cmp);
for (Component component : components) {
    if ((component instanceof JPopupMenu)) {
        JPopupMenu popupMenu = (JPopupMenu) component;
        popupMenu.addMenuKeyListener(keyListener);
    if ((component instanceof JMenuItem)) {
        JMenuItem menuItem = (JMenuItem) component;
...