I got the JMenu down except for removing. :D I mean, I can do popup.remove(NUMBER)
but that can cause NPE errors. So, is there a way to remove all JMenuItems
from JMenu
?
Here's my update checkPopup()
if anyone's interested:
private void checkPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
int itemSelectx = listbox.getSelectedIndex();
Object actItemx = listbox.getModel().getElementAt(itemSelectx);
System.out.println("You pressed on " + actItemx);
if (actItemx == "Item 1") {
popup.add(cancelMenuItem); // add the ability to cancel an item
popup.add(dropMenuItem); // add ability to drop the item
}
popup.show(inv.this, e.getX(), e.getY()); // show item at mouse
popup.revalidate(); // revalidat开发者_StackOverflowe
//popup.remove(0); // removing first (0) menu item
}
}
Almost there! :) (yes, I tried Google and JavaDocs)
If I've understood what you're after correctly, you want the removeAll()
method on JMenu
; See the Javadoc here.
精彩评论