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
}
}
}
}
}
}
}