1

So I would have asked this in the Java chat, but my new overflow account does not have 20 rep yet so I would like to ask it here. So basically my problem is sometimes when I open one of the JToggleButtons in my minesweeper application, it opens but does not set the text for close bombs or does not set the Icon to a bomb when pressed. This happens about every 5-8th click.

My method for opening the tiles just uses recursion and checks around for any bombs then keeps track of that, and if there is not a bomb, then it moves onto that tile

My setTileText method just sets the number of bombs that are close by.

So I have been debugging this and cant really pinpoint where the problem is, it might be in my mouseListener but I think it is probably here. If my mouseListener is needed to understand this better let me know. I really appriciate any help that is given, I have been trying to debug this for 3 days with no luck.

Thanks!

UPDATE:

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).

Here is my mouse listener for better understanding:

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
                                }
                            }
                        }
                    }
                }
            }
        }
Michi
  • 13
  • 4
  • It's hard for any of us to debug this code without a valid [mcve], but creating and posting one of these would take considerable effort on your part. If you don't get a decent answer soon, then you may have no choice. While awaiting an answer, please feel free to have a look at [my Java Swing MineSweeper program](https://stackoverflow.com/a/7016492/522444). – Hovercraft Full Of Eels Jun 17 '17 at 18:11
  • [A more recent version is here](https://stackoverflow.com/a/41072158/522444) – Hovercraft Full Of Eels Jun 17 '17 at 18:12
  • Yeah, I just dont know how to kinda really narrow down the problem in the code, its just that I know it has something to do when a JToggleButton is being opened, I think it may not be recognized or something since one thing I noticed is the timer does not start when this error happens on the first click. Its kind of like the button opens but does not continue to the openTiles and setTileText method untill I have to press it again - so I know it has to do with the way these three methods interact between each other. I am really confused as I just feel as though I have looked at everything. – Michi Jun 17 '17 at 18:45
  • At this point any suggestions on where to look or any help is greatly appreciated... I really am having trouble locating it as its random. Thanks! – Michi Jun 17 '17 at 18:49

0 Answers0