Java JMenu limitMenuSize(JMenu menu, String name, int size)

Here you can find the source of limitMenuSize(JMenu menu, String name, int size)

Description

This ensures that there are no more than size number of items in any sub menu.

License

Open Source License

Parameter

Parameter Description
menu The menu
name The name suffix to use
size Max size of a menu

Declaration

public static void limitMenuSize(JMenu menu, String name, int size) 

Method Source Code

//package com.java2s;
/*//w  w  w.j  av  a  2s.  co  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.util.ArrayList;

import java.util.List;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

public class Main {
    /**
     * 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.
     * It uses name (e.g., Group) to make the sub-menus, e.g., Group 1, Group 2, ...
     *
     * @param items List of JMenuItems
     * @param name The name suffix to use
     * @param size Max size of a menu
     */
    public static void limitMenuSize(List items, String name, int size) {
        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);
        }
    }

    /**
     * This ensures that there are no more than size number of items in any sub menu.
     * It uses name (e.g., Group) to make the sub-menus, e.g., Group 1, Group 2, ...
     *
     *
     * @param menu The menu
     * @param name The name suffix to use
     * @param size Max size of a menu
     */
    public static void limitMenuSize(JMenu menu, String name, int size) {
        limitMenuSize(menu, name, size, true);
    }

    /**
     * This ensures that there are no more than size number of items in any sub menu.
     * It uses name (e.g., Group) to make the sub-menus, e.g., Group 1, Group 2, ...
     *
     *
     * @param menu The menu
     * @param name The name suffix to use
     * @param size Max size of a menu
     * @param recurse If true then limit the size of all sub menus
     */
    public static void limitMenuSize(JMenu menu, String name, int size, boolean recurse) {
        int cnt = menu.getItemCount();
        List subMenus = new ArrayList();
        List subItems = new ArrayList();
        for (int i = 0; i < cnt; i++) {
            JMenuItem mi = menu.getItem(i);
            subItems.add(mi);
            if (mi instanceof JMenu) {
                subMenus.add(mi);
            }
        }
        if (recurse) {
            limitMenuSize(subMenus, name, size);
        }
        if (cnt >= size) {
            menu.removeAll();
            JMenu currentMenu = new JMenu(name + " 1");
            menu.add(currentMenu);
            for (int i = 0; i < subItems.size(); i++) {
                JMenuItem mi = (JMenuItem) subItems.get(i);
                if (currentMenu.getItemCount() >= size) {
                    currentMenu = new JMenu(name + " " + (menu.getItemCount() + 1));
                    menu.add(currentMenu);
                }
                if (mi == null) {
                    continue;
                }
                currentMenu.add(mi);
            }
        }
    }
}

Related

  1. getMenuItemIndex(JMenu menu, String menuItemText)
  2. getMenuItems(JMenu menu)
  3. getTextByJMenu(List lstMenus)
  4. insertSeparatorIfNeeded(JMenu menu, int position)
  5. isAtLeastOneChildComponentVisible(JMenu menu)
  6. makeMenu(JMenu menu, List menuItems)
  7. menuItem(JMenu parent, String label, Object... attrs)
  8. removeAllSeparators(JMenu menu)
  9. removeTopAndBottomSeparators(JMenu menu)