从JMenuItem中删除

时间:2011-02-02 16:38:08

标签: java swing jpopupmenu jmenu

好的,所以我终于得到了JListMenu的东西,谢谢你们。但是,现在我正在尝试使它具有SPECIFIC项目具有SPECIFIC命令。因此,类似于项目1将无法右键单击取消,但项目2,项目3和项目4可以。看看我要去哪里?

所以这是我目前的代码:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class inv extends JApplet implements MouseListener
{
    public JList listbox;
    public JPopupMenu popup;
    public JMenuItem item;

    public void init()
    {
        ActionListener menuListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                String invAction = event.getActionCommand();


                int itemSelect = listbox.getSelectedIndex();
                Object actItem = listbox.getModel().getElementAt(itemSelect);

                System.out.println("Popup menu item [" + invAction + "] [ " + actItem + " ] was pressed.");
            }
        };

        popup = new JPopupMenu();

        popup.add(item = new JMenuItem("Use"));
        item.addActionListener(menuListener);

        popup.add(item = new JMenuItem("Drop"));
        item.addActionListener(menuListener);

        popup.add(item = new JMenuItem("Cancel"));
        item.addActionListener(menuListener);



        String listData[] =
        {
            "Item 1","Item 2","Item 3","Item 4"
        };

        listbox = new JList( listData );
        listbox.addMouseListener( new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                if ( SwingUtilities.isRightMouseButton(e) )
                {
                    System.out.println("Row: " + getRow(e.getPoint()));
                    listbox.setSelectedIndex(getRow(e.getPoint()));
                }
            }
        }
        );

        listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(listbox);
        listbox.setVisible(true);
        listbox.setFocusable(false);


        listbox.addMouseListener(new MousePopupListener());
    }

    class MousePopupListener extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            checkPopup(e);
        }

        public void mouseClicked(MouseEvent e)
        {
            checkPopup(e);
        }

        public void mouseReleased(MouseEvent e)
        {
            checkPopup(e);
        }

        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);

                popup.show(inv.this, e.getX(), e.getY());
                popup.revalidate();
            }
        }
    }

    private int getRow(Point point)
    {
        return listbox.locationToIndex(point);
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }
}

并且,它有效!我的意思是,右键单击,您将看到您在哪个项目列表中选择的选项。但是,右键单击菜单项时会出现一个小警告标签。怪异。

Anwways,在System.out.println("You pressed on " + actItemx);下面我尝试添加此内容:

        if (actItemx == "Item 1") {
            System.out.println("Removed cancel for " + actItemx);
            popup.remove(2); // So upon right-click on Item 1, you won't see "Cancel" menu.
        }
它似乎工作(但它还没有完成,因为我必须重新添加它,嘿)。但是,当我右键单击时,取消已经消失。像预期一样。呜!但后来我再次右键单击它,我收到了这个错误:

Exception in thread "AWT-EventQueue-1" java.lang.IllegalArgumentException: index
 greater than the number of items.
        at javax.swing.JPopupMenu.remove(JPopupMenu.java:412)
        at inv$MousePopupListener.checkPopup(inv.java:102)
        at inv$MousePopupListener.mouseReleased(inv.java:88)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:2
73)
        at java.awt.Component.processMouseEvent(Component.java:6267)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
啊,好吧 - 回到绘图板。我做错了什么?

1 个答案:

答案 0 :(得分:2)

替换

popup.remove(2);

popup.remove(itemSelectx);