In order to associate a specific pop-up menu with a graphic
object, create a JPopupMenu
object and
link it to a graphic as follows:
graphic.setPopupMenu(popupMenu);
Pop-up menus only work when the view is registered with
the pop-up menu manager. In order to enable pop-up menus in a manager
view, register the view as follows:
IlvPopupMenuManager.registerView(managerView);
After the pop-up menu is registered, whenever a user
right-clicks the graphic, its pop-up menu appears.
Pop-up menus use a lot of memory. To avoid wasting memory,
share pop-up menus between multiple graphic objects. To do this, instead
of registering a pop-up menu with an individual graphic using graphic.setPopupMenu(...)
,
register the pop-up menu directly with the pop-up menu manager by
calling:
IlvPopupMenuManager.registerMenu("name", popupMenu);
You then assign this pop-up menu to graphics in the following
way:
graphic1.setPopupMenuName("name");
graphic2.setPopupMenuName("name");
Pop-up menus registered with a single graphic object
are active for that object only. Pop-up menus registered with the
pop-up menu manager can be:
When a pop-up menu is shared, you need to know which
graphic object triggered the event. The action listeners associated
with pop-up menu items can retrieve the context of the pop-up menu
using an
IlvPopupMenuContext object. This is done in the following way:
public void actionPerformed(ActionEvent e) {
JMenuItem m = (JMenuItem)e.getSource();
IlvPopupMenuContext context=IlvPopupMenuManager.getPopupMenuContext(m);
if (context == null) return;
IlvGraphic graphic = context.getGraphic();
IlvManagerView view = context.getManagerView();
//Do the action on this graphic for this view.
}
IlvSimplePopupMenu
is a subclass
of
JPopupMenu
that allows you to configure
pop-up menus easily. For more information, see
IlvSimplePopupMenu.