0

So basically I am writing a minesweeper game currently and am having a problem with my JToggleButtons. Sometimes when they are pressed (like every 10%), they do this random thing where they open, but do not follow/execute the mouseListener method for them until I have to press them again. This applies to both my right click and left click feature so I am assuming that it has something to do how JToggleButtons interact with mouseListener but I am honestly not sure. I have been trying to debug this for days, but its really weird since it randomly happens whether it be twice in a row, or after 20 normal openings, etc. Any comments, ideas and help to fix this is greatly appreciated!

An example is most of the time, I click the button and it does all it needs to do (set the tile text to the close bombs, open the nearby tiles which are empty, etc.), but when this error happens, it just setSelects to true and does nothing (not even entering the MouseAction listener). My action listener is as follows:

public void mouseClicked(MouseEvent a) {

        if (SwingUtilities.isLeftMouseButton(a)){   

            boolean found = false;
            int i = 0, j = 0;

            for (i = 0; i < length; i++)
            {
                for (j = 0; j < width; j++)
                {
                    System.out.println("x:"+j);                 
                    System.out.println("y"+i);

                    if (a.getSource() == tiles[i][j])
                    {
                        if (!hasIcon(tiles[i][j]) && userPlay)
                        {
                            tiles[i][j].setIcon(null);
                            tiles[i][j].setSelected(false);
                            return;
                        }
                        found = true;
                        break;
                    }
                }
                if (found) break;
            }

            if(userPlay){

                tiles[i][j].setSelected(true);

                if(!firstClick)
                {
                    spawnMines(i,j);
                    timer.start(watch);
                    firstClick = true;
                }

                if(tileType[i][j]!= -1)
                {
                    openTiles(i,j);
                    setTileText();
                } 
                else lose();

                checkWin();
            } 
            else setTileText();
        }

        else if (SwingUtilities.isRightMouseButton(a))
        {   
            int i = 0, j = 0;

            for (i = 0; i < length; i++)
            {
                for (j = 0; j < width; j++)
                {
                    System.out.println("x:"+j);                 
                    System.out.println("y"+i);

                    if (a.getSource() == tiles[i][j])
                    {
                        if (hasIcon(tiles[i][j]) && (tileType[i][j] == 0 || tileType[i][j] == -1) && userPlay)
                        {
                            tiles[i][j].setIcon(flag);
                            bombTracker--;
                            bombNumber.setText("                      # of Bombs: "+ bombTracker); //text Area used for bombNumber
                        }
                        else
                        {
                            if (userPlay&&(tileType[i][j]!=-2))
                            { 
                                tiles[i][j].setIcon(null);
                                bombTracker++;
                                bombNumber.setText("                      # of Bombs: "+ bombTracker); //text Area used for bombNumber
                            }
                        }
                    }
                }
            }
        }
    }

Thanks!

Michi
  • 13
  • 4
  • 1
    Please post [mcve] – c0der Jun 18 '17 at 05:38
  • I'm left asking the question of "why?" - Why aren't you using an `ActionListener` instead? Why aren't you monitoring the selected state of the button? What benefit is the `MouseListener` providing you over these other methods? – MadProgrammer Jun 18 '17 at 05:54
  • I am using mouse listener since my left click has the function of opening tiles while my right click has the function of flagging... I am just not really sure how to differentiate between the two different mouse controls in actionListener... Hope that clears your question @MadProgrammer?? Thanks! – Michi Jun 18 '17 at 06:21
  • *"I hope that makes it more understandable.."* It's not an MCVE / SSCCE as suggested by @c0der. Voting to close. – Andrew Thompson Jun 18 '17 at 08:17
  • @Michi Then maybe consider not using button based component, which already has a defined mouse operation, just saying – MadProgrammer Jun 18 '17 at 08:42
  • This might help: https://stackoverflow.com/questions/2006188/right-click-on-jbutton – c0der Jun 18 '17 at 08:48

0 Answers0