Solved

masking the right click functionality in JMenu

Posted on 2001-06-11
8
596 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
Comment Utility
subclass it and catch the MouseEvent if its the right button.
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:YamunaiThuraivan
Comment Utility
good answer
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
Thanks, hope is what you want?
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
so ... are you gonna accept the answear ?
0
 
LVL 9

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
noX challenge 17 75
create a gui in perl 3 42
unix in java example 9 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now