Java Swing Menu Item appendMenuItem(Component menuItem, StringBuilder builder, String indent)

Here you can find the source of appendMenuItem(Component menuItem, StringBuilder builder, String indent)

Description

append Menu Item

License

Open Source License

Declaration

private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.awt.Component;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

import javax.swing.JSeparator;

import javax.swing.MenuElement;

public class Main {
    static final String NEWLINE = System.lineSeparator();

    private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) {
        if (menuItem instanceof JMenuBar) {
            appendMenuSubElements((MenuElement) menuItem, builder, indent);
        } else if (menuItem instanceof JPopupMenu) {
            appendMenuSubElements((MenuElement) menuItem, builder, indent);
        } else if (menuItem instanceof JMenuItem) {
            builder.append(NEWLINE);/*from  www .j  av a  2s  . c  om*/
            builder.append(indent);
            builder.append(((JMenuItem) menuItem).getText());
            builder.append(((JMenuItem) menuItem).isEnabled() ? "" : "[Disabled]");
            appendMenuSubElements((JMenuItem) menuItem, builder, "|   " + indent);
        } else if (menuItem instanceof JSeparator) {
            builder.append(NEWLINE);
            builder.append(indent);
            builder.append("--------");
        }
    }

    private static void appendMenuSubElements(MenuElement element, StringBuilder builder, String indent) {
        for (MenuElement subItem : element.getSubElements()) {
            appendMenuItem((Component) subItem, builder, indent);
        }
    }
}

Related

  1. addMenuItem(Container menu, String text, ActionListener listener)
  2. addMenuItem(final Action action, final C topLevelMenu, final String... path)
  3. createMenu(String menu, String[] menuItemNames)
  4. createMenuItem(Action a)
  5. createMenuItem(Action action, Icon icon)
  6. createMenuItem(ActionListener listener, String cmd, String toolTip)