下面列出了javax.swing.JMenu#getMenuComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convenience method that recursively enables every JMenu and JMenuItem inside
* "menu".
*
* @param menu - the menu to start the recursive search
*/
public static void enableAll(JMenu menu) {
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
Component x = menu.getMenuComponent(i);
if (x instanceof JMenuItem)
((JMenuItem) x).setEnabled(true);
else if (x instanceof JMenu)
enableAll((JMenu) x);
}
}
private static void setMnemonics(final JMenuItem item) {
if (item instanceof JMenu) {
final JMenu menu = (JMenu) item;
final int c = menu.getMenuComponentCount();
final ArrayList<JMenuItem> list = new ArrayList<JMenuItem>(c);
for (int i = 0; i < c; i++) {
final Component co = menu.getMenuComponent(i);
if (co instanceof JMenuItem)
list.add((JMenuItem) co);
}
if (list.size() > 0)
setMnemonics(list);
}
}
/**
* Remove any potential orphan separator at the end of the menu.
*
* @param menu the menu to purge
*/
public static void trimSeparator (JMenu menu)
{
int count = menu.getMenuComponentCount();
if ((count > 0) && menu.getMenuComponent(count - 1) instanceof JSeparator) {
menu.remove(count - 1);
}
}
/**
* Take any given memu object and turn on scrollbars if it contains more than 20 items.
* Then, cycle through all submenus recursively.
*
* @param menu
*/
public static void createScrollBarsOnMenus(JMenu menu) {
if (menu.getMenuComponentCount() > 20) {
MenuScroller.setScrollerFor(menu, 20);
}
for (int i = 0; i < menu.getMenuComponentCount(); i++) {
if (menu.getMenuComponent(i) instanceof JMenu) {
MenuScroller.createScrollBarsOnMenus(((JMenu)menu.getMenuComponent(i)));
}
}
}
/**
* Add a separator to a menu if useful.
*
* @param menu Menu.
* @return Number of items added.
*/
public int addSeparator(JMenu menu) {
if ((menu == null) || (menu.getMenuComponentCount() == 0)) {
return 0;
}
Component item = menu.getMenuComponent(menu.getMenuComponentCount() - 1);
if (!(item instanceof JMenuItem)) {
return 0;
}
menu.add(new JSeparator());
return 1;
}
@Override
public void finishMenu(String path) {
path = mapping(path);
if (path.equals("_") || path.startsWith("_/")) {
return;
}
if (!menuElements.containsKey(path)) {
throw new IllegalArgumentException("Invalid menu: " + path);
}
if (path.startsWith("/file/recent")) {
return;
}
MenuElement me = menuElements.get(path);
if (me instanceof JMenu) {
JMenu jm = (JMenu) me;
if (jm.getMenuComponentCount() == 1) {
String parentPath = path.contains("/") ? path.substring(0, path.lastIndexOf('/')) : "";
MenuElement parMe = menuElements.get(parentPath);
JMenuItem mi = (JMenuItem) jm.getMenuComponent(0);
jm.remove(mi);
if (parMe instanceof JMenu) {
JMenu parMenu = (JMenu) parMe;
parMenu.remove(jm);
parMenu.add(mi);
} else if (parMe instanceof JMenuBar) {
JMenuBar parMenuBar = (JMenuBar) parMe;
parMenuBar.remove(jm);
parMenuBar.add(mi);
}
}
}
}
/**
* Remove any potential orphan separator at the end of the menu.
*
* @param menu the menu to purge
*/
public static void trimSeparator (JMenu menu)
{
int count = menu.getMenuComponentCount();
if ((count > 0)
&& menu.getMenuComponent(count - 1) instanceof JSeparator) {
menu.remove(count - 1);
}
}
/**
* Sets the menu font.
*
* @param m a menu
* @param font the font
*/
private static void setMenuFont(JMenu m, Font font) {
m.setFont(font);
for(int i = 0; i<m.getMenuComponentCount(); i++) {
m.getMenuComponent(i).setFont(font);
if(m.getMenuComponent(i) instanceof JMenu) {
setMenuFont((JMenu) m.getMenuComponent(i), font);
}
}
}