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