0

I tried to add a actionlistener to my JMenuItems for my right click. I seen many example they use item.addActionListener(this) to add a listener to the JMenuItem

 JMenuItem item;
 item.addActionListener(this);

But however I tried it out and I am getting the error

 the method addActionListener(ActionListener) in the type AbstractButton is not 
 applicable for the arguements (PopUpMenuExample)

error

code

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class PopUpMenuExample extends JPanel {

  public JPopupMenu popup;

  public PopUpMenuExample() {
      popup = new JPopupMenu();

     JMenuItem item;
     popup.add(item = new JMenuItem("Add"));
     item.setHorizontalTextPosition(JMenuItem.RIGHT);
     item.addActionListener(this); <-- error!

     popup.add(item = new JMenuItem("Delete"));
     item.setHorizontalTextPosition(JMenuItem.RIGHT);
     item.addActionListener(this); <-- error!

     popup.setLabel("Justification");
     popup.setBorder(new BevelBorder(BevelBorder.RAISED));
     popup.addPopupMenuListener(new PopupPrintListener());

     addMouseListener(new MousePopupListener());
  }

  // An inner class to check whether mouse events are the popup trigger
  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()) {
           popup.show(PopUpMenuExample.this, e.getX(), e.getY());
       }
    }
  }

  public static void main(String s[]) {
     JFrame frame = new JFrame("Popup Menu Example");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setContentPane(new PopUpMenuExample());
     frame.setSize(300, 300);
     frame.setVisible(true);
  }
}

Edited(adding JMenuitem in the JPopUpMenu)

table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
                System.out.println("Right Click");
                int r = table.rowAtPoint(e.getPoint());
                if (r >= 0 && r < table.getRowCount()) {
                    table.setRowSelectionInterval(r, r);

                } else {
                    table.clearSelection();
                }

                int rowindex = table.getSelectedRow();
                if (rowindex < 0)
                    return;
                if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
                    JPopupMenu popup = new JPopupMenu();
                    JMenuItem item;
                    popup.add(item = new JMenuItem("Add"));
                    item.setHorizontalTextPosition(JMenuItem.RIGHT);
                    item.addMouseListener(this);
                    popup.add(item = new JMenuItem("Delete"));
                    item.setHorizontalTextPosition(JMenuItem.RIGHT);;
                    popup.setComponentPopupMenu(popup);
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
user2211678
  • 669
  • 8
  • 13
  • 24

2 Answers2

4

this is a JPanel which does not implement ActionListener...

Have a look at How to Write an Action Listeners and How to Use Menus and you might as well become farmiluar with How to Use Actions

You can also use JComponent#setComponentPopupMenu instead of using a MouseListener and you should be making sure that you are building your UIs from within the context of the Event Dispatching Thread, have a look at Initial Threads for more details

Popup Example

Works just fine for me...

JTable table = new JTable(model);

JPopupMenu popupMenu = new JPopupMenu();
JMenuItem mi = popupMenu.add("Boo!");
mi.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Ah, a ghost!");
    }
});
table.setComponentPopupMenu(popupMenu);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @mKorbel I thought that to, but I'm trying to be nice to people...for the next five minutes... – MadProgrammer Aug 19 '14 at 08:19
  • then I can see there another 2-3 issues more important that there is missing implements ActionListener – mKorbel Aug 19 '14 at 08:20
  • +1 thank you, but your 5 minutes with pace_full_face is gone :-) – mKorbel Aug 19 '14 at 08:33
  • sorry I think I need a little more help. now I tried to use the JMenuItem in my JTable. I popup.setComponentPopUpMenu(pop) and popup.show(e.getComponent,e.getX() ,e.getY()) in my JTable. I right click but the PopUpMenu does not show. I edited my codes – user2211678 Aug 19 '14 at 08:35
1

You have to add implement ActionListener and implement the method actionPerform

public class PopUpMenuExample extends JPanel implements ActionListener
Jens
  • 67,715
  • 15
  • 98
  • 113
  • You "could" `implement ActionListener`...you could also use anonymous inner class, inner class or an external class which implements `ActionListener` (ah, the good old days) or an `Action` ... all of which would be acceptable... – MadProgrammer Aug 19 '14 at 08:20