An efficient way of adding JMenuItems

1.6k views Asked by At

OK so before my INV program was to REMOVE menu items but then I was all like.. that's too much. So what if I were to ADD menu items for specific Right-clicked items INSTEAD of removing every time?

So, if you right-clicked on Item 1, you'd get "Use" and "Drop" added to the menu. Then ONCE you choose your option, the JMenu would delete everything so it would be right where we started. Then if you right-clicked on ITem 2, it would add "Use" and "Cancel". See where I'm going?

I tried doing it myself, but I just can't figure out how to do it -- for example, to add a new JMenuItem, you need to do this:

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

and, as you can see, add an actionlistener. I can't do that under if (actItemx == "Item 1") { so... what do I do?

Anyways, here's what I have so far:

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

            if (actItemx == "Item 1") {
                System.out.println("Removed cancel for " + actItemx);
                popup.remove(itemSelectx); // So upon right-click on Item 1, you won't see "Cancel" menu.
            }

                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)
    {
    }
}
2

There are 2 answers

0
jluzwick On BEST ANSWER

What about setting up different JPopup menu's for each type of item. What I mean by that is you have something that looks like this:

public JPopupMenu useDropPopup;
public JPopupMenu useCancelPopup;

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.");
            }
        };

   useDropPopup = new JPopupMenu();
   useCancelPopup = new JPopupMenu();

   JMenuItem useMenuItem = new JMenuItem("Use");
   useMenuItem.addActionListener(menuListener);
   JMenuItem dropMenuItem = new JMenuItem("Drop");
   dropMenuItem.addActionListener(menuListener);
   JMenuItem cancelMenuItem = new JMenuItem("Cancel");
   cancelMenuItem.addActionListener(menuListener);

   useDropPopup.add(useMenuItem);
   useDropPopup.add(dropMenuItem);

   useCancelPopup.add(useMenuItem);
   useCancelPopup.add(cancelMenuItem);

   // ... etc bring up the appropriate popup depending on the item.
}

Also, you should not assign a JMenuItem to item inside of a method call. That's bad practice. Also consider using different actionlisteners for each menu item so you can separate the functional code of each menu item, ex:

useMenuItem.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent event) {
      useMenuAction(event);
   }
};

// ... after the init method

public void useMenuAction(ActionEvent evt) {
   // Add specific use menu code here.
}
0
Paŭlo Ebermann On

This is mostly an addition to the answer from jluzwick:

Instead of creating a JMenuItem and adding an ActionListener to it, you can use a Action - this is basically a combination of a ActionListener with a name, optionally Icon and some other properties. (Most simply extend from AbstractAction, overriding the actionPerformed-Method.)

Then add the Action to your JMenu, and it will construct the JMenuItem itself. (You can also use the same Action object on other places, like Buttons, the "normal" menu bar, etc.)