title: MouseListener to Instance of Game Panel
body: <p>I'm creating a Tic Tac Toe game where I'm using MouseListener to add functionality to my game panel. When a user clicks one of the cells its suppose to generate an X or O graphic depending on whose turn it is. I've tried adding the MouseListener to the pane, but when I run it nothing happens when I click. Any ideas on how I can fix this?</p>
  
  <p>Here's my game panel:</p>
  
  <pre class="lang-java prettyprint-override"><code>public GameMain() {
          Handler handler = new Handler();
          this.addMouseListener(handler);
  
          // setup JLabel
          label = new JLabel("         ");
          label.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5));
          label.setOpaque(true);
          label.setBackground(Color.LIGHT_GRAY);
  
          setLayout(new BorderLayout());
          add(label, BorderLayout.SOUTH);
          setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT + 30));
          board = new Board();
          initGame();
  
      }   
  }
  </code></pre>
  
  <p>Here's my Handler class with the mouseClick() method that's suppose to run:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Handler extends MouseAdapter {
          public void mouseClick(MouseEvent e) {
              int mouseX = e.getX();
              int mouseY = e.getY();
  
              // Get the row and column clicked
              int rowSelected = mouseY / CELL_SIZE;
              int colSelected = mouseX / CELL_SIZE;
  
              if (currentState == GameState.PLAYING) {
                  if (rowSelected &gt;= 0 &amp;&amp; rowSelected &lt; board.cells.length &amp;&amp; colSelected &gt;= 0 &amp;&amp; colSelected &lt; board.cells.length &amp;&amp;  
                      board.cells[rowSelected][colSelected].content == Seed.EMPTY) {
                      board.cells[rowSelected][colSelected].content = currentPlayer; // move
                      updateGame(currentPlayer, rowSelected, colSelected); // update currentState
                      currentPlayer = (currentPlayer == Seed.X) ? Seed.O : Seed.X;
                  }
              } else {
                  initGame();
              }
  
              repaint();
          }
  
          public void handleButtonPress(Object o) {
              if (o == singlePlayer) {
                  singlePlayerGame();
              }
              if (o == multiPlayer) {
                  multiPlayerGame();
              }
          }
      }
  </code></pre>