1

I am a beginner at java so am attempting to create a minesweeper game. I have a grid full of JButtons that when clicked reveal numbers or mines.

I want to add "flags" so when I right click on any button it changes colour - showing it has been flagged and is 'locked' so it cannot be clicked on unless right clicked.

Here are my buttons

public void buttons()
{
    // Creating the grid with buttons
    grid.setLayout(new GridLayout(20,20));
    // Button grid
    for (int x = 0; x < buttons.length; x++)
    {
        for (int y = 0; y < buttons.length; y++)
        {
            buttons[x][y] = new JButton();
            buttons[x][y].addActionListener(this);
            grid.add(buttons[x][y]);
        }
    }

I attempted to create this right click functionality but I am currently stuck and would like some help.

public void flag()
{
    buttons.addMouseListener(new MouseAdapter() 
    {
        public void mousePressed(MouseEvent e) 
        {
            JButton rightClick = new JButton("Right Click");
            rightClick.addActionListener(this);
        }
    });
}
JOG
  • 15
  • 3
  • and by being 'stuck' you mean what exactly? – blurfus Dec 18 '14 at 22:32
  • I'm not sure how to go about doing this (right-clicking a button and when you do it changes colour). I am aware that I need MouseListeners and MouseAdapters but I'm not sure how – JOG Dec 18 '14 at 22:34
  • check out `MouseEvent` http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html - you will need to detect clicking of button 2 – blurfus Dec 18 '14 at 22:36

1 Answers1

1

Typically, you would process user events on a button via the button's ActionListener, what you want to try and do is stop the user from trigger the buttons ActionListener, the easiest way that you can do this is disable the button...

Normally, I don't like using MouseListeners on buttons as this is not normally the best way to ascertain user interaction, but in this case, it's the requirement

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton btn;

        public TestPane() {

            setBorder(new EmptyBorder(8, 8, 8, 8));

            setLayout(new GridBagLayout());
            btn = new JButton("O");
            btn.setMargin(new Insets(8, 8, 8, 8));
            add(btn);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("You clicked me");
                }
            });

            btn.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {
                        btn.setEnabled(!btn.isEnabled());
                        if (btn.isEnabled()) {
                            btn.setText("O");
                        } else {
                            btn.setText("X");
                        }
                    }
                }
            });

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366