开发者

Remove all JMenuItem from JMenu

开发者 https://www.devze.com 2023-02-08 00:40 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消