Java JPopupMenu removeExtraSeparators(JPopupMenu popup)

Here you can find the source of removeExtraSeparators(JPopupMenu popup)

Description

Removes extra separators, if any.

License

Open Source License

Parameter

Parameter Description
popup the popup menu.

Declaration

public static void removeExtraSeparators(JPopupMenu popup) 

Method Source Code

//package com.java2s;
import javax.swing.*;

import java.awt.*;

public class Main {
    /**//from  www .  java2 s  .co m
     * Removes extra separators, if any. This can be used when you remove some menu items and leave extra separators on
     * the UI.
     *
     * @param popup the popup menu.
     */
    public static void removeExtraSeparators(JPopupMenu popup) {
        Component[] components = popup.getComponents();
        if (components.length <= 1) {
            return;
        }
        for (int i = 0; i < components.length; i++) {
            Component component = components[i];
            if (component instanceof JSeparator) {
                if (i == 0 || i == components.length - 1) { // if the separator is the first one or the last one, remove it because the separator is not necessary here
                    popup.remove(component);
                } else if (components[i - 1] instanceof JSeparator) {
                    popup.remove(component);
                }
            }
        }
    }
}

Related

  1. optimizeSeparators(JPopupMenu menu)
  2. positionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord)
  3. positionPopupMenu(final JPopupMenu popupMenu, final MouseEvent event, final Rectangle rectangle, final int dividerlocation)
  4. refreshPopup(JPopupMenu popup)
  5. removeConsecutiveSeparators(JPopupMenu popupMenu)
  6. removeTopAndBottomSeparators(JPopupMenu popupMenu)
  7. setLocationOnScreen(JPopupMenu visibleMenu, int x, int y)
  8. setPopupMenu(final JComponent component, final JPopupMenu popup)
  9. setPopupMenu(final JComponent component, final JPopupMenu popup)