2

I'm not sure if this a Mac issue, or an issue with my code. I am creating a grid of buttons. For each button I use ActionEvent for a regular click, and MouseEvent for a right click. What happens is when I CTRL-click the mouse event performs fine, however the action even also fires. Is there a way I can get around this while also using both action and mouse events? Relevant code:

View Constructor:

for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                button[i][j] = new Cell();
                button[i][j].addActionListener( new changeButtonHandler() );
                button[i][j].addMouseListener( new handleRight() );
                playArea.add(button[i][j]);

            }
        }

Action Event Class:

public class changeButtonHandler implements ActionListener
    {
        /**
         * Action performed after button is clicked
         * 
         */
        @SuppressWarnings("unchecked")
        public void actionPerformed(ActionEvent e)
        {

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (button[i][j] == e.getSource())
                    {   
                    //do stuff
                        }
                        else if(button[i][j].mine==false){
                            //do other stuff
                        }   
                    }

                }
            }   
        }   
    }//end changeButtonHandler class

Mouse Event Class

public class handleRight implements MouseListener {

           /**
             * Action performed after button is right-clicked
             * 
             */
        public void mouseClicked(MouseEvent e)
        {
            if (SwingUtilities.isRightMouseButton(e) || e.isControlDown())      {
                System.out.println("Right Worked");
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        if (button[i][j] == e.getSource())
                        {   
                                  //do stuff
                        }
                    }
                }
            }
        }
Michael Ramos
  • 5,523
  • 8
  • 36
  • 51
  • In a situation like this, you're better off creating a simple program, one without an array of buttons, but one that is small but complete enough for us to compile, run and test, a [minimal example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Mar 07 '14 at 22:37
  • So you want the Mouse event to fire with a ctrl-click and not have the button pressed? That is an unusual behavior, at least to me. – Hovercraft Full Of Eels Mar 07 '14 at 22:58
  • CTRL-Click on a Mac acts as a right-click would in Windows, so in a game where a right-click action matters, then yes that is how I would like for it to work. (I don't use a mouse) – Michael Ramos Mar 07 '14 at 23:02

1 Answers1

3

When I tried to reproduce your problem with my own minimal example program, I couldn't. The MouseListener worked when expected and the ActionListener worked when expected:

import java.awt.event.*;
import javax.swing.*;

public class TestButtonRightClick {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JButton button = new JButton("Test Me!");
            button.addActionListener(new ActionListener() {

               @Override
               public void actionPerformed(ActionEvent e) {
                  System.out.println("ActionListener invoked");
               }
            });
            button.addMouseListener(new MouseAdapter() {
               @Override
               public void mousePressed(MouseEvent e) {
                  if (e.getButton() == MouseEvent.BUTTON3) {
                     System.out.println("Right Button Pressed");
                  }
               }
            });

            JPanel panel = new JPanel();
            panel.add(button);
            JOptionPane.showMessageDialog(null, panel);
         }
      });
   }
}

Edit: Why use SwingUtilities instead of e.getMouseButton()?

// ? SwingUtilities
if (SwingUtilities.isRightMouseButton(e) || e.isControlDown())      {

Note, for further help, consider creating your own minimal example program similar to mine above.


Edit 2

To check the state of the ctrl key on button press, check the modifiers of the ActionEvent in your ActionListener:

           @Override
           public void actionPerformed(ActionEvent e) {
              if ((e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
                 System.out.println("control pressed");
              } else {
                 System.out.println("ActionListener invoked");
              }
           }
        });
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Then I'm thinking my problem might be when using CTRL-Click on the Mac, I am going to test using a mouse and Windows. – Michael Ramos Mar 07 '14 at 22:45
  • 1
    Ah yes, the problem is a CTRL-Click issue, otherwise with a mouse it works just fine. – Michael Ramos Mar 07 '14 at 22:49
  • 1
    @Mrambo: see **Edit 2**. The solution is to modify your ActionListener not the MouseListener. – Hovercraft Full Of Eels Mar 07 '14 at 23:02
  • This is what I was looking for. Thank you. And also thanks for letting me know in future reference to create a minimal example program.. instead of down voting. – Michael Ramos Mar 07 '14 at 23:05
  • @Mrambo: You're welcome. It just makes it so much easier to help when a decent one has been created and posted. For example look at [this guy's question](http://stackoverflow.com/q/22261579/522444) from today. It's his first question here and he posted a damn nice minimal example. Kudos to him! – Hovercraft Full Of Eels Mar 07 '14 at 23:07