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

JMenuItemaddMenuItem(JPopupMenu menu, Action action)
add Menu Item
JMenuItem item = menu.add(action);
configureMenuItem(item, action);
return item;
voidaddMenuItems(JPopupMenu menu, JComponent... items)
add Menu Items
for (JComponent item : items) {
    if (item == null) {
        menu.addSeparator();
    } else {
        menu.add(item);
KeyStrokeaddOption(JPopupMenu popupMenu, String text, String key, ActionListener actionListener)
add Option
JMenuItem option = new JMenuItem(text);
KeyStroke keyStroke = KeyStroke.getKeyStroke(key);
option.setAccelerator(keyStroke);
option.addActionListener(actionListener);
popupMenu.add(option);
return keyStroke;
voidaddPopup(Component component, final JPopupMenu popup)
add Popup
component.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            showMenu(e);
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
...
voidaddPopup(Component component, final JPopupMenu popup)
Add the specified pop-up menu the specified component.
component.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger())
            showMenu(e);
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger())
            showMenu(e);
...
voidaddPopup(final Component c, final JPopupMenu m)
add Popup
c.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger())
            m.show(c, e.getX(), e.getY());
});
voidaddSeparatorIfNecessary(JPopupMenu popup)
Adds a separator to the popup menu if there are menu items on it already.
int count = popup.getComponentCount();
if (count > 0 && !(popup.getComponent(count - 1) instanceof JSeparator)) {
    popup.addSeparator();
voidaddSeparatorIfNecessary(JPopupMenu popup)
add Separator If Necessary
int count = popup.getComponentCount();
if ((count > 0) && (!(popup.getComponent(count - 1) instanceof JSeparator)))
    popup.addSeparator();
booleanaddSeparatorIfNeeded(JPopupMenu popupMenu)
Appends a separator to the end of the menu if it exists at least one non separator menu component immediately before and if there isn't, already, a separator at the end of the menu.
final int menuComponentCount = popupMenu.getComponentCount();
if (menuComponentCount == 0) {
    return false;
final Component lastMenuComponent = popupMenu.getComponent(menuComponentCount - 1);
if (isPopupMenuSeparator(lastMenuComponent)) {
    return false;
popupMenu.addSeparator();
return true;
voidapplyDefaultProperties(final JPopupMenu comp)
Sets default background and foreground color as well as a default font for the specified component.
if (comp == null) {
    return;
applyProperties(comp, "PopupMenu.background", 
        "PopupMenu.foreground", 
        "PopupMenu.font");