How to Create Popup Menu in Java Swing

A popup menu is a free-floating menu which associates with an underlying component. This component is called the invoker.  Most of the time, popup menu is linked to a specific component to display context-sensitive choices.

In order to create a popup menu, you use the class JPopupMenu. You then can add menu itemsJMenuItem to popup menu like normal menu. To display the popup menu, you call method show().Normally popup menu is called in response to a mouse event. Here is the code to show the poup menu:

In the code above, we check to see whether the popup is triggered. If not we pass the component and location where the popup menu is being displayed to the show() method of JPopupMenu instance.

Here is the JPopupMenu demo application screenshot:

Java Popup Menu

  • Was this tutorial helpful ?
  • YesNo