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

voidrefreshPopup(JPopupMenu popup)
Mysterious calls to pack(), invalidate() and validate() ;-)
popup.pack();
popup.invalidate();
Component c = popup.getParent();
if (c != null) {
    c.validate();
voidremoveConsecutiveSeparators(JPopupMenu popupMenu)
Removes all consecutive separators from the given menu.
for (int i = 1; i < popupMenu.getComponentCount(); i++) {
    if (isPopupMenuSeparator(popupMenu.getComponent(i))) {
        if (isPopupMenuSeparator(popupMenu.getComponent(i - 1))) {
            popupMenu.remove(i);
            i--;
voidremoveExtraSeparators(JPopupMenu popup)
Removes extra separators, if any.
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) { 
...
voidremoveTopAndBottomSeparators(JPopupMenu popupMenu)
Convenience method that calls the methods removeTopSeparators(JPopupMenu) and removeBottomSeparators(JPopupMenu) with the given pop up menu as parameter.
if (popupMenu.getComponentCount() == 0) {
    return;
removeTopSeparators(popupMenu);
removeBottomSeparators(popupMenu);
voidsetLocationOnScreen(JPopupMenu visibleMenu, int x, int y)
This method exists because popup menus can not be directly moved (the have to be hidden and re-shown).
Point invokerLocation = visibleMenu.getInvoker().getLocationOnScreen();
visibleMenu.setVisible(false);
visibleMenu.show(visibleMenu.getInvoker(), x - invokerLocation.x, y - invokerLocation.y);
voidsetPopupMenu(final JComponent component, final JPopupMenu popup)
Gives a component a popup menu
component.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(final MouseEvent e) {
        if (e.getButton() > MouseEvent.BUTTON3)
            return;
        if (e.isPopupTrigger()) {
            popup.show(component, e.getX(), e.getY());
    @Override
    public void mouseReleased(final MouseEvent e) {
        if (e.getButton() > MouseEvent.BUTTON3)
            return;
        if (e.isPopupTrigger()) {
            popup.show(component, e.getX(), e.getY());
});
voidsetPopupMenu(final JComponent component, final JPopupMenu popup)
Gives a component a popup menu
component.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(final MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(component, e.getX(), e.getY());
    @Override
...
voidshowButtonPopupMenu(AbstractButton button, JPopupMenu popup)
show Button Popup Menu
if (!popup.isVisible()) {
    Dimension size = button.getSize();
    popup.show(button, 0, size.height);
voidshowMenu(JPopupMenu menu, JButton button)
Shows specified menu positioned relatively to specified button.
Dimension dim = menu.getPreferredSize();
menu.show(button, button.getWidth() - dim.width, button.getHeight());
voidshowModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt)
show Modal
java.awt.Component root = javax.swing.SwingUtilities.getRoot(invoker);
final javax.swing.JLayeredPane layeredPane;
if (root instanceof javax.swing.JFrame) {
    javax.swing.JFrame window = (javax.swing.JFrame) root;
    layeredPane = window.getLayeredPane();
} else if (root instanceof javax.swing.JDialog) {
    javax.swing.JDialog window = (javax.swing.JDialog) root;
    layeredPane = window.getLayeredPane();
...