The problem is a JPopupMenu
is not a component that is initially visible or added to a container. So by just dragging and dropping it into the design view frame, will have no affect visually to the design view.
But, if you look at the source code or the navigator, you will see the jPopupMenu
declared as a class member and instantiated in the initComponents()
method.
I've attempted different things myself, and from what I've tried, it doesn't look like you can design the popup menu in a visual way. You can though, use the Navigator to design it.
- From the Navigator, you will see the
jPopupMenu1
. You can addJMenus
orJMenuItems
by right-clicking it and selectingAdd from Palette
. - You can then add listener to the
JMenuItem
by right clicking theJMenuItem
from the navigator and slectingEvents -> Actions -> actionPerformed
To make the JPopupMenu
appear, you need to add a MouseListener
to a component, whether it's the frame or another component. For example (to the frame):
- Select the frame from the Navigator and right-click it and select
Events -> Mouse ->
and you will need to implement bothmousePressed
andmouseReleased
, as different platforms have different popup triggers, Windows beingmouseReleased
and I think Mac ismousePressed
(don't quote me). Create a method to show the popup menu.
private void showPopupMenu(MouseEvent e) { jPopupMenu1.show(this, e.getX(), e.getY()); }
Implement your
mousePressed
andmouseReleased
methodsprivate void formMousePressed(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } } private void formMouseReleased(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } }