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
}
}
}
}
}
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:
Edit: Why use SwingUtilities instead of
e.getMouseButton()
?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: