Michi
Michi

Reputation: 13

JToggleButton opening but not Executing MouseListener Method

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!

Upvotes: 0

Views: 76

Answers (0)

Related Questions