• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}

}


 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks, surely will take care of this.

With Regards
Nitin
 
I don't always make ads but when I do they're tiny
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic