The JPopupMenu Class
Pop-up menus are an increasingly popular user-interface feature. These menus are not attached to a menu bar; instead, they are free-floating menus that associate themselves with an underlying component. This component is called the invoker . Linked to specific interface elements, pop-up menus are nicely context-sensitive. They are brought into existence by a platform-dependent pop-up trigger event that occurs while the mouse is over the invoking component. In AWT and Swing, this trigger is typically a mouse event. Once raised, the user can interact with the menu normally. Figure 14-9 is an example of a pop-up menu in Swing.

Figure 14-9. A pop-up menu in Swing
You can add or insert JMenuItem, Component, or Action objects to the pop-up menu with the
add( ) and insert( ) methods.
The JPopupMenu class assigns an
integer index to each menu item and orders them based on the layout
manager of the pop-up menu. In addition, you can add separators to the
menu by using the addSeparator( )
method; these separators also count as an index. Figure 14-10 shows the class
diagram for the JPopupMenu component.
Starting with SDK 1.4, pop-up menus use the Popup class to actually draw themselves. This
class is also used for other briefly displayed interface elements like
tooltips.
Figure 14-10. JPopupMenu class diagram
Displaying the Pop-up Menu
Pop-up menus are usually raised by ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access