Solved

masking the right click functionality in JMenu

Posted on 2001-06-11
8
601 Views
Last Modified: 2010-05-18
How do i hide the poping up of menutiems when i rightclick a JMenu which should occur only with the leftclick?
0
Comment
Question by:YamunaiThuraivan
  • 6
8 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6178438
subclass it and catch the MouseEvent if its the right button.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6180390
subclass the JMenu and your JMenuItems and overwrite the portected void processMouseEvent(MouseEvent me) method to disallow right click. Im this method you should get a 'true' value for a call to me.isPopupTrigger() method;

If doesn't work with me.isPopupTrigger() method, you should test the which mouse button was presses and if is the right one just return. (All this in the same processMouseEvent method). If you don't overwrite'it will be called with the default implementation.
0
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 6180470
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//Disabling the right click on a popup item

public class Popup extends JFrame {
     JMenuBar bar;
 
     public Popup() {
          init();
     }
     
     private void init() {
          getContentPane().setLayout(new BorderLayout());
          setSize(400, 300);
          setLocation(300, 100);
    bar = new JMenuBar();
   
    MyMenu menu = new MyMenu("File");
    menu.add(new JMenuItem("Open"));
          menu.add(new JMenuItem("Save"));
    bar.add(menu);
   
          menu = new MyMenu("Edit");
    menu.add(new JMenuItem("Copy"));
          menu.add(new JMenuItem("Paste"));
    bar.add(menu);    
          menu = new MyMenu("Tools");
          menu.add(new JMenuItem("Tool1"));
          menu.add(new JMenuItem("Tool2"));
          bar.add(menu);    
          menu = new MyMenu("Help");
          menu.add(new JMenuItem("Help"));
          menu.add(new JMenuItem("About"));
          bar.add(menu);    
    setJMenuBar(bar);
          setVisible(true);
     }
     
  class MyMenu extends JMenu {  
       public MyMenu(String name) {
            super(name);
       }
   
       protected void processMouseEvent(MouseEvent me) {
      int modifiers = me.getModifiers();
         if(me.getID() == MouseEvent.MOUSE_PRESSED)  {
           if(modifiers == MouseEvent.BUTTON3_MASK)
             return;
         }
     
            super.processMouseEvent(me);
       }
  }
 
     public static void main(String[] args) {
          Popup t = new Popup();    
     }
     
}
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 9

Expert Comment

by:Ovi
ID: 6180487
The problem is that the menu is showed on a mousePressed event and the isPopupTrigger() method is true on a mouseReleased event. This is the case of Windows. From what I know in Linux is the reverse order. So the complete code for the method to avoid platform problems is :

       protected void processMouseEvent(MouseEvent me) {
      int modifiers = me.getModifiers();
         if(me.getID() == MouseEvent.MOUSE_PRESSED || me.getID() == MouseEvent.MOUSE_RELEASED)  {
           if(modifiers == MouseEvent.BUTTON3_MASK)
             return;
         }
     
            super.processMouseEvent(me);
       }

I've tested my self and is working. If you have submenus on the File menu for example, you must do the same thing as subclassing the JMenuItem class and overwriting the same method.
0
 

Author Comment

by:YamunaiThuraivan
ID: 6185109
good answer
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6185115
Thanks, hope is what you want?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6185357
so ... are you gonna accept the answear ?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6190387
Thank you very much for that BBBBBBBBBBBBBBBBBBBBBB. DID YOU HAVE ANY PROBLEMS WITH THE CODE ?
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
eclipse java  build path 6 57
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
How do I remove an object from a 3 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question