Java Utililty Methods JPopupMenu

List of utility methods to do JPopupMenu

Description

The list of methods to do JPopupMenu are organized into topic(s).

Method

JComponentgetPopupMenu()
get Popup Menu
return popupMenu;
PointgetPopupMenuShowPoint(JPopupMenu popup, int x, int y)
Returns a point where the given popup menu should be shown.
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y + sizeMenu.height);
Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
    Rectangle screenBounds = screensBounds[i];
    if (screenBounds.x <= x && x <= (screenBounds.x + screenBounds.width)) {
        Dimension sizeScreen = screenBounds.getSize();
...
booleaninsertSeparatorIfNeeded(JPopupMenu popupMenu, int position)
Inserts a separator at the given position if it exists a non separator menu component at the given position and if there isn't, already, a separator immediately before the insert position (to prevent consecutive separators).
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;
voidinstallPopupMenuColorAndFonts(final JComponent contentPane)
install Popup Menu Color And Fonts
LookAndFeel.installColorsAndFont(contentPane, "PopupMenu.background", "PopupMenu.foreground",
        "PopupMenu.font");
booleanisPopupMenuSeparator(Component component)
Tells whether or not the given component is a JPopupMenu.Separator .
return (component instanceof JPopupMenu.Separator);
JPopupMenumakePopupMenu(JPopupMenu menu, List menuItems)
Create a JPopupMenu and add the menus contained with the menus list If no menus then return null.
if ((menuItems == null) || (menuItems.size() == 0)) {
    return null;
for (int i = 0; i < menuItems.size(); i++) {
    Object o = menuItems.get(i);
    if (o.toString().equals(MENU_SEPARATOR)) {
        menu.addSeparator();
    } else if (o instanceof JMenuItem) {
...
voidmaybeShowPopup(MouseEvent e, JPopupMenu menu, JComponent parent)
Check if the popup menu must be show and show it
if (e.isPopupTrigger()) {
    menu.show(parent, e.getX(), e.getY());
voidoptimizeSeparators(JPopupMenu menu)
optimize Separators
boolean lastSeparator = true;
for (int i = 0; i < menu.getComponentCount();) {
    if (menu.getComponent(i).isVisible() && menu.getComponent(i) instanceof JMenu)
        optimizeSeparators(((JMenu) menu.getComponent(i)).getPopupMenu());
    boolean separator = menu.getComponent(i) instanceof JPopupMenu.Separator;
    if (lastSeparator && separator)
        menu.remove(i);
    else
...
voidpositionPopup(Component component, JPopupMenu jpm, int xCoord, int yCoord)
position Popup
Dimension popupSize = jpm.getSize();
if (popupSize.width == 0)
    popupSize = jpm.getPreferredSize();
Point point = new Point(xCoord + popupSize.width, yCoord + popupSize.height);
SwingUtilities.convertPointToScreen(point, component);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = 0;
int y = 0;
...
voidpositionPopupMenu(final JPopupMenu popupMenu, final MouseEvent event, final Rectangle rectangle, final int dividerlocation)
Position a Popup Menu relative to the selected node, but always so that the left edge is not beyond the tree panel given by dividerlocation.
int intPopupLeft;
intPopupLeft = (int) rectangle.getX() + (int) rectangle.getWidth();
if (intPopupLeft > dividerlocation) {
    intPopupLeft = dividerlocation - 50;
popupMenu.show(event.getComponent(), intPopupLeft,
        (int) rectangle.getY() + ((int) rectangle.getHeight() >> 1));