除了移除外,我把JMenu关闭了。 :D我的意思是,我可以做popup.remove(NUMBER)
但这会导致NPE错误。那么,有没有办法从JMenuItems
删除所有JMenu
?
如果有兴趣的话,这是我的更新checkPopup()
:
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(); // revalidate
//popup.remove(0); // removing first (0) menu item
}
}
几乎就在那里! :)(是的,我tried Google和JavaDocs)