Link to home
Start Free TrialLog in
Avatar of galneweinhaw
galneweinhaw

asked on

How to single-out a right-click for a JMenuItem

currently, my menu item has the following:                        

JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem("...");
menuItem.setSelected(....isEnabled());
menuItem.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent e) {
                                    new mYDialog(....).setVisible(true);
                              }
}
/*
however, on a left-click I would like to toggle the checkbox of the menu item.  I would like to keep all the other actions as normal....left-click opens menu, Enter opens menu etc.


Thanks!
Avatar of sciuriware
sciuriware

in the 'actionPerformed()' ask what click it was.

;JOOP!
Avatar of galneweinhaw

ASKER

How?

I would think

if (e ==?)

but from the info on ActionEvents:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html

I can't see what I need =(
I was going to use paramString() but that says it's for debgging only.  I don't see how else to do it though.
sorry, I was wrong.

See : isRightMouseButton(MouseEvent anEvent);

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how about

if(e.getID() == Event.MOUSE_DOWN)

???
looking at something like this:

                              public void actionPerformed(ActionEvent e) {
                                    
                                    if(e.getID() == Event.MOUSE_DOWN) {
                                          menuItem.setSelected(menuItem.isSelected()? false : true );

how do I get access to the menuItem from inside actionPerformed?
"To be combined with the 'actionPerformed()' that knows which screen button / menu item it was"

and how do I do this?

I can call
menuItem.setSelected(menuItem.isSelected()? false : true );
inside actionPerformed.

Avatar of Mick Barry
try:

JCheckBoxMenuItem item = (JCheckBoxMenuItem) event.getSource();
>>> how do I get access to the menuItem from inside actionPerformed?

I adapted the design pattern to create a GUI class per program:

public class MyGui extends Thread implements ActionListener
{
......................
private JButton b;
......................
       void createGui()
       {
                b = new JButton("Press");
                b.addActionListener(this);
.............................
       public void actionPerformed(ActionEvent e)
       {
         Object o = e.getSource();

         if(o == b)
         {
// .... and from here I have access to all components and variables.

;JOOP!

Ahhhh, you can not even have a coffee-break in this forum!!

;JOOP!
P.S.: the 'extends Thread' is there to even incorporate timers and asynchroneous processes
into the context. Please ignore that topic.
The pattern idea is: make your class listener to everything you are interested in,
so you can access all your administration from within.
Anonymous classes and separate listener classes have the problem you mentioned: proper access.

;JOOP!
                             public void actionPerformed(ActionEvent e) {
                                    
                                    if (e.getID() == Event.MOUSE_DOWN){
                                          JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
                                          item.setSelected(item.isSelected()? false: true);
                                    }

ok
e.GetID() is giving 501
Event.MOUSE_DOWN is giving 1001

??
oops, other way around:
e.GetID() is giving 1001
Event.MOUSE_DOWN is giving 501

even if I change it to:

                      public void actionPerformed(ActionEvent e) {
                             
                              if (e.getID() == 1001){
                                   JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
                                   item.setSelected(item.isSelected()? false: true);
                              }

the check box doesn't toggle =( nothing happens at all
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And a remark:
                      public void actionPerformed(ActionEvent e) {
                             
                              if (e.getID() == 1001){
                                   JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
                                   item.setSelected(item.isSelected()? false: true);
                              }
Did you check that e.getSource() really obtained the right control (the checkbox)? No, you assumed it.
Always say:
                       Object o = e.getSource();

                       if(o == <mycontrol>)
                       {
                                // Then it is from that control.

;JOOP!
thanks sciuriware, I will do.

question tho, what's the point of GetID if it will always return 1001?

(I was just using the hard coded number to check the rest of the code =)  which helped, cus I got the check box thing working)

now I just need it only on left-click.  Which will happen when I try your solution!
if(o == <mycontrol>)

how do I know what <mycontrol> is? the whole point of getting it .getSource is so it tells me what control it is...isn't it?

anyway, that part is working now =)
If you want to check things like these, usr a visual debugger (ECLIPSE) or print it out to System.out.println();
The use of getId()? I searched the JAVA sources and I found that you should not call getId() in actionPerformed()
as it will always give this PRO FORMA value.
You should use the methods of ActionEvent() to get more info.

;JOOP!
if(o == .....)
YES! that's the way to find out what control is firing!!!!!!!!!       see a snippet of one of my sources:

   /** (non-Javadoc)
    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    * @param a action event.
    */
   public void actionPerformed(ActionEvent a)
   {
      Object o = a.getSource();

      if(o == abnButton)
      {
         ChiThuHelp.setHelp(ChiThuHelp.HELP_ABN);
         activatePane(abnPane, CARD_ABN);
      }
      else if(o == ccdButton)
      {
         ChiThuHelp.setHelp(ChiThuHelp.HELP_CCD);
         activatePane(ccdPane, CARD_CCD);
      }
      else if(o == corrButton)
      {
so...

                        JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(vision.getLabel());
                        menuItem.setSelected(vision.isEnabled());

                        menuItem.addMouseListener( new MouseAdapter() {
                              public void mouseClicked(MouseEvent me) {
                                    
                                    if(SwingUtilities.isRightMouseButton(me)) {
                                          // reverse whatever the current enablement is
                                          vision.setEnabled(!vision.isEnabled());
                                    }
                              }
                        });

When debugging, this doesn't even go into mouseClicked, when I click the item,  is there something wrong with the way I used new MouseAdapter?
by changing mouseClicked to mousePressed..it now works!

public void mousePressed(MouseEvent e) {

..any idea why?
Do not wonder why the component (here the menuItem) chooses to call a
certain method on the listener.
I built some custom components with listeners and sometimes you don't
know to choose.
That's easier with ActionListener: only one callback: actionPerformed().

So, now you're done?

;JOOP!
yes, thanks a ton for your help!
Thanks to you too, I learned from this too, because I had to recover my knowledge.
;JOOP!