title: java keylistener not called
body: <p>I have written a sample code using <code>KeyListener</code> in Java,
  I have created a <code>JPanel</code>, then set its focusable to true, I have created a KeyListener, requested a focus and then added the KeyListener to my panel. But the methods for the keyListener are never called. It seems although I have requested focus, it does not focus.</p>
  
  <p>Can anyone help? </p>
  
  <pre><code>listener = new KeyLis();
  this.setFocusable(true);
  this.requestFocus();
  this.addKeyListener(listener);
  
   class KeyLis implements KeyListener{
  
      @Override
      public void keyPressed(KeyEvent e) {
          currentver += 5;
           switch (e.getKeyCode()) {
              case KeyEvent.VK_LEFT : if(horizontalyInBounds()) currentPos-= 5;  
                   break;
              case KeyEvent.VK_RIGHT: if(horizontalyInBounds()) currentPos+= 5;  
                   break;
          }
          repaint();
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void keyTyped(KeyEvent e) {
      }
  }
  </code></pre>
  
  <p>If any runnable code should be needed:</p>
  
  <pre><code>  import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
  
    import javax.swing.JFrame;
    import javax.swing.JLabel;
  
  
   public class test extends JFrame {
  
  private AreaOfGame areaOfGame;
  
  public test()
  {
      super("");
      setVisible(true);
      this.setBackground(Color.darkGray);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.pack();
      setLayout(null);
      setBounds(200, 10, 400, 700);
  
      areaOfGame = new AreaOfGame();
      this.add(areaOfGame);
  
      startGame();
  }
  
  public int generateNext()
  {
      Random r = new Random();
      int n = r.nextInt(7);
      return n;
  }
  
  public void startGame()
  {
      while(!areaOfGame.GameOver())
      {
          areaOfGame.startGame(generateNext());
      }
  }
  
  
  public static void main(String[] args) {
      new MainFrame();
  }
  
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  
  import javax.swing.JPanel;
  
  public class AreaOfGame extends JPanel {
  
  
      private static final int rightside = 370;
  
      private int bottom;
      private int top;
  
      private int currentPos;
      private int currentver;
      private KeyLis listener;
  
      public AreaOfGame()
      {
          super();
  
          bottom = 650;
          top = 50;
          setLayout(null);
          setBounds(20, 50, 350, 600);
          setVisible(true);
  
  
          this.setBackground(Color.lightGray);
  
          listener = new KeyLis();
          this.setFocusable(true);
          if(this.requestFocus(true))
              System.out.println("true");;
          this.addKeyListener(listener);
  
  
          currentPos = 150;
          currentver=0;
      }
  
      public void startGame(int n)
      {
          while(verticallyInBound()){
              System.out.println("anything");
  
          }
  
  
      }
  
      public boolean verticallyInBound()
      {
          if(currentPos&lt;= bottom -50)
              return true;
          return false;
      }
  
  
      public boolean GameOver()
      {
          if(top&gt;= bottom){
              System.out.println("game over");
              return true;
          }
  
          else return false;
      }
  
  
      public boolean horizontalyInBounds()
      {
          if(currentPos&lt;=rightside &amp;&amp; currentPos&gt;= 20)
              return true;
          else return false;
      }
  
  
  class KeyLis implements KeyListener{
  
          @Override
          public void keyPressed(KeyEvent e) {
              System.out.println("called");
              currentver += 5;
               switch (e.getKeyCode()) {
                  case KeyEvent.VK_LEFT : if(horizontalyInBounds()) currentPos-= 5;  break;
                  case KeyEvent.VK_RIGHT: if(horizontalyInBounds()) currentPos+= 5;  break;
              }
              repaint();
  
  
          }
  
          @Override
          public void keyReleased(KeyEvent e) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void keyTyped(KeyEvent e) {
              System.out.println("called 3");
          }
  }
  
  }
  </code></pre>