• Post Reply Bookmark Topic Watch Topic
  • New Topic

JMenuItem: Not allowing Mouse Right Click to function on JMenuItem.

 
nitin goswami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please help me out. My problem is, As per the current implementation of my application, when I Right Click on a JTable Row, A Popup menu appears to give me an option to "delete" the row. On "delete" JMenuItem -> I want only left mouse click should work and if someone presses left click or any othe button, it shud not react and the popup menu should remain as it is.

I tried to acheive this with MouseListener but when user clicks any other button then "left" operation doesn't happen but at the same time menu also disappears.

So Currently I am using ActionListenr on JMenuItem.

JPopupMenu popup = new JPopupMenu();
JMenuItem headItem = new JMenuItem("Permission Value");
popup.add(headItem);
popup.addSeparator();

JMenuItem menuItem = new JMenuItem("Delete: " + permissionTypeValueEntity.getValueAt(0));
popup.add(menuItem);
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
String valueToDelete = permissionTypeValueEntity.getValueAt(0);
permissionTypeActionListener.deletePermissionTypeValue(valueToDelete);
model.removeEntity(permissionTypeValueEntity);
int indexToNext = table.getRowCount()-1;
// Once a value is deleted, immediate previous value should become the selected one.
permissionTypeActionListener.setSelectedPermissionValueIndex(indexToNext);
permissionTypeActionListener.loadPermissionTypeValues(permissionTypeActionListener.getSelectedPermissionType());
table.scrollRowToVisible(indexToNext);
}
catch (PermissionServiceException pse) {
MessageDialog dialog = MessageDialog.createInstance(PermissionValueListPanel.this, true);
dialog.showErrorMessage("User Message", pse.getMessage());
}
}
});

popup.show(e.getComponent(), e.getX(), e.getY());
 
nitin goswami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Following is the solution(found in one of the tech forum). This will not allow Right Click to operate on JMenuItem.

class TMGenericMenuItem extends JMenuItem
{

public TMGenericMenuItem(String str){
super(str);
}

protected void processMouseEvent(MouseEvent e)
{
if (e.getModifiers() != MouseEvent.BUTTON3_MASK)
{
super.processMouseEvent(e);
}
}

}


 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's great that you found a solution yourself

However, could you please Use Code Tags in the future? Thanks.
 
nitin goswami
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks, surely will take care of this.

With Regards
Nitin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!