code: public static boolean insertSeparatorIfNeeded(JPopupMenu popupMenu, int position) {
          final int menuComponentCount = popupMenu.getComponentCount();
          if (menuComponentCount == 0 || position <= 0 || position > menuComponentCount) {
              return false;
          }
          final Component currentComponent = popupMenu.getComponent(position);
          if (isPopupMenuSeparator(currentComponent)) {
              return false;
          }
          final Component previousComponent = popupMenu.getComponent(position - 1);
          if (isPopupMenuSeparator(previousComponent)) {
              return false;
          }
          popupMenu.insert(new JPopupMenu.Separator(), position);
          return true;
      }
docs: Inserts a separator at the given {@code position} if it exists a non separator menu component at the given
  {@code position} and if there isn't, already, a separator immediately before the insert {@code position} (to prevent
  consecutive separators).
  
  @param popupMenu the pop up menu that will be processed
  @param position the position where a separator might be inserted
  @return {@code true} if the separator was inserted, {@code false} otherwise.
  @see javax.swing.JPopupMenu.Separator