Right click on JButton

Button can't be pressed by right click. Add such a lines to you mouse listener

mousePressed:

if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));

mouseReleased:

if(needReset) {underlyingButton.getModel().setPressed(false));

or do there whatever want.


As you have mentioned that checking for "mousePressed" solved your issue. And the Javadoc of isPopupTrigger would explain the need for this:

public boolean isPopupTrigger()
...
Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.

Also see the section on The Mouse Listener API in the Java Swing tutorial.


I wouldn't use isPopupTrigger but directly check for the right button:

button.addMouseListener(new MouseAdapter(){
  public void mouseClicked(MouseEvent e){
    boolean mine = field.isMine(x, y);
    if (e.getButton() == MouseEvent.BUTTON2) {
      button.setText("F");
    }
  ...

Just a small addition: the simplest way to check for the right button is SwingUtilities.isRightMouseButton