Java JMenu removeTopAndBottomSeparators(JMenu menu)

Here you can find the source of removeTopAndBottomSeparators(JMenu menu)

Description

Convenience method that calls the method removeTopAndBottomSeparators(JPopupMenu) with the JPopupMenu of the given menu as parameter.

License

Apache License

Parameter

Parameter Description
menu the menu whose top and bottom separators will be removed

Declaration

public static void removeTopAndBottomSeparators(JMenu menu) 

Method Source Code


//package com.java2s;
/*/*w  ww .  j  av a2  s  . co m*/
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2014 The ZAP Development Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Component;

import javax.swing.JMenu;
import javax.swing.JPopupMenu;

public class Main {
    /**
     * Convenience method that calls the methods {@code removeTopSeparators(JPopupMenu)} and
     * {@code removeBottomSeparators(JPopupMenu)} with the given pop up menu as parameter.
     * 
     * @param popupMenu the pop up menu whose top and bottom separators will be removed
     * @see #removeTopSeparators(JPopupMenu)
     * @see #removeBottomSeparators(JPopupMenu)
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeTopAndBottomSeparators(JPopupMenu popupMenu) {
        if (popupMenu.getComponentCount() == 0) {
            return;
        }
        removeTopSeparators(popupMenu);
        removeBottomSeparators(popupMenu);
    }

    /**
     * Convenience method that calls the method {@code removeTopAndBottomSeparators(JPopupMenu)} with the {@code JPopupMenu} of
     * the given {@code menu} as parameter.
     * 
     * @param menu the menu whose top and bottom separators will be removed
     * @see #removeTopAndBottomSeparators(JPopupMenu)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeTopAndBottomSeparators(JMenu menu) {
        removeTopAndBottomSeparators(menu.getPopupMenu());
    }

    /**
     * Removes all top separators from the given pop up menu.
     * <p>
     * For example, calling the method on the given menu:
     * <pre>
     *    Separator
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     * </pre>
     * would result in:
     * <pre>
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     * </pre>
     * </p>
     * 
     * @param popupMenu the pop up menu whose top separators will be removed
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeTopSeparators(JPopupMenu popupMenu) {
        while (popupMenu.getComponentCount() > 0 && isPopupMenuSeparator(popupMenu.getComponent(0))) {
            popupMenu.remove(0);
        }
    }

    /**
     * Convenience method that calls the method {@code removeTopSeparators(JPopupMenu)} with the {@code JPopupMenu} of the given
     * {@code menu} as parameter.
     * 
     * @param menu the menu whose top separators will be removed
     * @see #removeTopSeparators(JPopupMenu)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeTopSeparators(JMenu menu) {
        removeTopSeparators(menu.getPopupMenu());
    }

    /**
     * Removes all bottom separators from the given pop up menu.
     * <p>
     * For example, calling the method on the given menu:
     * <pre>
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Separator
     * </pre>
     * would result in:
     * <pre>
     *    Menu Entry
     *    Separator
     *    Menu Entry
     *    Separator
     *    Menu Entry
     * </pre>
     * </p>
     * 
     * @param popupMenu the pop up menu whose bottom separators will be removed
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeBottomSeparators(JPopupMenu popupMenu) {
        int indexLastComponent = popupMenu.getComponentCount() - 1;
        while (indexLastComponent >= 0 && isPopupMenuSeparator(popupMenu.getComponent(indexLastComponent))) {
            popupMenu.remove(indexLastComponent);
            indexLastComponent -= 1;
        }
    }

    /**
     * Convenience method that calls the method {@code removeBottomSeparators(JPopupMenu)} with the {@code JPopupMenu} of the
     * given {@code menu} as parameter.
     * 
     * @param menu the menu whose bottom separators will be removed
     * @see #removeBottomSeparators(JPopupMenu)
     * @see JMenu#getPopupMenu()
     * @see javax.swing.JPopupMenu.Separator
     */
    public static void removeBottomSeparators(JMenu menu) {
        removeBottomSeparators(menu.getPopupMenu());
    }

    /**
     * Tells whether or not the given {@code component} is a {@code JPopupMenu.Separator}.
     * 
     * @param component the component that will be checked.
     * @return {@code true} if the given component is a {@code JPopupMenu.Separator}, {@code false} otherwise.
     * @see javax.swing.JPopupMenu.Separator
     * @see #isExtensionPopupMenu(Component)
     * @see #isExtensionPopupMenuItem(Component)
     */
    public static boolean isPopupMenuSeparator(Component component) {
        return (component instanceof JPopupMenu.Separator);
    }
}

Related

  1. isAtLeastOneChildComponentVisible(JMenu menu)
  2. limitMenuSize(JMenu menu, String name, int size)
  3. makeMenu(JMenu menu, List menuItems)
  4. menuItem(JMenu parent, String label, Object... attrs)
  5. removeAllSeparators(JMenu menu)
  6. setChildrenEnabled(JMenu menu, boolean enabled)
  7. tearDownMenu(JMenu menu)