'MouseListener interfering with ButtonListener, causing the mousePressed functions commands to be ignored?

I'm currently working on a GUI for a Ticktacktoe game and I have a 3x3 grid of JButtons all with ButtonListeners and MouseListeners. When the buttons are disabled the MouseListener works as intended and changes the color of the button upon a mouse press and reverts back to an original color when the mouse is released. However when the buttons become active, the color becomes blue when the button is pressed, not the color I have it set to with the mousePressed function. I have asked around and have been told that maybe the reason is the ButtonListeners are overriding the MouseListeners. I will attach my code below:

The Button initialization and MouseListener

        b1 = new JButton();
        b2 = new JButton();
        b3 = new JButton();
        b4 = new JButton();
        b5 = new JButton();
        b6 = new JButton();
        b7 = new JButton();
        b8 = new JButton();
        b9 = new JButton();
    
        bg[0][0] = b1;
        bg[0][1] = b2;
        bg[0][2] = b3;
        bg[1][0] = b4;
        bg[1][1] = b5;
        bg[1][2] = b6;
        bg[2][0] = b7;
        bg[2][1] = b8;
        bg[2][2] = b9;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
             button_grid.add(bg[i][j]);
             bg[i][j].addActionListener(this);
             bg[i][j].setText("");
             bg[i][j].setEnabled(false);
             bg[i][j].setBackground(new Color(241, 244, 198));
             JButton jButton1= bg[i][j];
             jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton1.setBackground(new Color(170, 166, 164));
            }
    
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton1.setBackground(new Color(241, 244, 198));
            }
            
            
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1.setBackground(new Color(100, 10, 100));
            }
            
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1.setBackground(new Color(241, 244, 198));
            }
            });
                   
           }
        }

ButtonListeners

@Override
    public void actionPerformed(ActionEvent e)
    {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (e.getSource() == bg[i][j])
                {
                   play_sound(clip);
                   bg[i][j].setBackground(Color.BLACK);
                   add_mark(bg[i][j], i, j);
                }
            }
        }
          if (e.getSource() == newgame) {
            play(); 
        }

        else if (e.getSource() == quitgame) {
            System.exit(0); 
        }
        
    }

I have tried looking for functions that change the color of the button upon a press in the JButton Class but have had no luck. If you know how to fix this, I would appreciate an answer.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source