views:

375

answers:

8

Hi guys,

I am trying to write a Minesweeper clone in Java for fun. I have a grid of JButtons whose labels I will change to represent the danger count, flags, etc.

My problem is, I don't know how to get a right click on a JButton to depress the button. I've done the following:

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

This doesn't seem to be working at all; the "F" is never shown, only the "X" part. But more importantly, this does nothing for depressing the button.

EDIT: Macs have popup trigger happen on mousePress, not mouseClick.

EDIT: Here's the solution I worked out based off of accepted answer:

button.addMouseListener(new MouseAdapter(){
            boolean pressed;

            @Override
            public void mousePressed(MouseEvent e) {
                button.getModel().setArmed(true);
                button.getModel().setPressed(true);
                pressed = true;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
                button.getModel().setArmed(false);
                button.getModel().setPressed(false);

                if (pressed) {
                    if (SwingUtilities.isRightMouseButton(e)) {
                        button.setText("F");
                    }
                    else {
                        button.setText("X");
                    }
                }
                pressed = false;

            }

            @Override
            public void mouseExited(MouseEvent e) {
                pressed = false;
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                pressed = true;
            }                    
        });
        add(button);

Minesweeper clone

+1  A: 

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseEvent.html

MouseEvent has some properties

static int BUTTON1
static int BUTTON2
static int BUTTON3

among others. Check those when your event fires.

EDIT

public int getButton()

Returns which, if any, of the mouse buttons has changed state. 
nc3b
Thanks, I can get which button was pressed, but that's no longer my problem.
I82Much
A: 

The button being visibly depressed on right click isn't part of the "normal" behavior of buttons. You may be able to fake it using JToggleButtons, or simply changing the button's background color and maybe border while the right mouse button is being held down.

Carl Smotricz
+1  A: 

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");
    }
  ...
perdian
A: 

If you are certain that the event is properly being triggered (debug FTW!) and that the button.setText("F") is happening, then perhaps you simply need to repaint.

Repaint the button. http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)

Tim Drisdelle
See my edit - not the problem any more.
I82Much
+1  A: 

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.

sateesh
A: 

This works for me fine on Mac:

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

public class ButtonTest extends JFrame {

    JButton button;

    public ButtonTest() {
        button = new JButton("W");
        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 3) { // if right click
                    button.setText("F");
                    button.getModel().setPressed(false);
                    // button.setEnabled(true);
                } else {
                    button.setText("X");
                    button.getModel().setPressed(true);
                    // button.setEnabled(false);
                }
            }
        });
        this.add(button);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ButtonTest();
    }

}

You might as well check for e.getButton() == 2 but I don't know when this one is triggered on Macs.

Artur
+1  A: 

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.

Rastislav Komara
I will try this tonight; looks most promising.
I82Much
In order for the visual cues to work correctly, need to call setArmed() before setPressed()
I82Much
A: 

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

eugener
This does not work for ctrl+click on mac which usually triggers right click.
I82Much