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

voidattachPopupMenu(final JComponent component, final JPopupMenu popupMenu)
Attach popup menu on the given component.
component.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
            popupMenu.show(e.getComponent(), e.getX(), e.getY());
});
...
voidcleanPopupLayoutMetrics(JPopupMenu popupMenu)
Cleans the layout metrics of the specified popup menu.
if (popupMenu != null) {
    popupMenu.putClientProperty(LAYOUT_METRICS, null);
PointcomputePopupLocation(MouseEvent event, Component rel, JPopupMenu popup)
Swing 1.0 and 1.1 have a horrible bug with respect to popup menus.
Dimension psz = popup.getSize();
Dimension ssz = Toolkit.getDefaultToolkit().getScreenSize();
Point gLoc = rel.getLocationOnScreen();
Point result = new Point(event.getX(), event.getY());
gLoc.x += event.getX();
gLoc.y += event.getY();
if (psz.width == 0 || psz.height == 0) {
    int items = popup.getSubElements().length;
...
JPopupMenucreatePopupMenu()

Create a JPopupMenu instance.

JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new JLabel("Help"));
popupMenu.addSeparator();
popupMenu.add(new JLabel("About"));
return popupMenu;
voidcreatePopupMenu(String menuText, JPopupMenu menu, ActionListener listener)
create Popup Menu
menu.add(createMenuItem(menuText, listener));
voidensurePopupIsOnScreen(JPopupMenu popup)
ensure Popup Is On Screen
java.awt.Point location = popup.getLocation(null);
java.awt.Dimension size = popup.getSize(null);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
screenSize.height -= 28; 
javax.swing.SwingUtilities.convertPointToScreen(location, popup);
if (location.x < 0) {
    location.x = 0;
} else if (location.x + size.width > screenSize.width) {
...
ComponentfindMenuComponent(JPopupMenu menu, String menuComponentName)
find Menu Component
for (int i = 0; i < menu.getComponentCount(); i++) {
    Component component = menu.getComponent(i);
    if (menuComponentName.equals(component.getName()))
        return component;
return null;
JMenufindSubMenu(JPopupMenu popupMenu, String name)
find Sub Menu
int n = popupMenu.getComponentCount();
for (int i = 0; i < n; i++) {
    Component c = popupMenu.getComponent(i);
    if (c instanceof JMenu) {
        JMenu subMenu = (JMenu) c;
        if (name.equals(subMenu.getName())) {
            return subMenu;
        subMenu = findSubMenu(subMenu.getPopupMenu(), name);
        if (subMenu != null) {
            return subMenu;
return null;
MouseAdaptergetMousePopupAdapter(final Component component, final JPopupMenu popup)
Creates a mouse popup adapter for the given component and popup menu.
return new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        handlePopup(e);
    @Override
    public void mouseReleased(MouseEvent e) {
        handlePopup(e);
...
JPopupMenugetOnlyPopupMenu(Container owner)
get Only Popup Menu
return (JPopupMenu) getOnlyComponent(owner, JPopupMenu.class);