title: Coloring a JButton in Java
body: <p>I am struggling with a problem on coloring an array of JButtons. </p>
  
  <p>I made two arrays of JButtons:</p>
  
  <pre><code>public JButton Speler1[] = new JButton[140]; //Player1
  public JButton Speler2[] = new JButton[140]; //Player2
  </code></pre>
  
  <p>These two arrays of buttons make lane 1 and lane 2 of a racing game. I want the position of player 1 and 2 to be colored on both screens. So player 1 can see where player 2 is and vice versa.</p>
  
  <p>I already made a method which sends the position of both players to eachother. </p>
  
  <pre><code>if (message.contains("Positie")) {
     String posit = message.replaceFirst("Positie", "");
     int positi = Integer.valueOf(posit);
     positie2 = positi;
     kleurHokje kleur = new kleurHokje();
     kleur.hokVerkleur(positi); // positi is the position of each player
  }
  </code></pre>
  
  <p>So when I call the method hokVerkleur(positi), I want to change a button on lane 2.</p>
  
  <pre><code>class kleurHokje{
      public void hokVerkleur(int loc){
          Speler2[loc].setBackground(Color.yellow);
          Speler2[positie2].setBackground(Color.gray);                
      }
  }
  </code></pre>
  
  <p>It just wont work. While i'm doing almost the same for Speler1[positie] except Speler1 doesn't use the network, which works as I want.</p>
  
  <p>Any help is appreciated,</p>
  
  <p>Thanks Jef</p>
  
  <p>Edit: 
  If I place my code in one of the MouseListeners it works fine, but rather have it to be colored automatically instead of having to click each time.</p>
  
  <pre><code> class Klaar extends MouseAdapter {
  
      public void mouseClicked(MouseEvent e) {            
          Speler2[positie2].setBackground(Color.gray);
      }
  }
  </code></pre>
  
  <p>Ps. my first language isn't english, I hope you understand my problem.</p>