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");
}
}
});