Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

masking the right click functionality in JMenu

Posted on 2001-06-11
8
Medium Priority
?
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

730 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