Solved

masking the right click functionality in JMenu

Posted on 2001-06-11
8
608 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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:

617 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