Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

masking the right click functionality in JMenu

Posted on 2001-06-11
8
Medium Priority
?
614 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 150 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 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