Question

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(); // revalidate
            //popup.remove(0); // removing first (0) menu item
        }
    }

Almost there! :) (yes, I tried Google and JavaDocs)

Was it helpful?

Solution

If I've understood what you're after correctly, you want the removeAll() method on JMenu; See the Javadoc here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top