title: Mouse Listener not working with a JFrame in Java
body: <p>I'm trying to make a 2D game in Java. I need a way to detect mouse input for player attacks and some other stuff. I already have a working Key Listener in my game but when I tried adding a Mouse Listener the same way I did the Key Listener it doesn't work.</p>
  
  <p>Here is my Mouse Listener class (Just some test codes for now and these line are never output in the console even when spamming my mouse everywhere on the display)</p>
  
  <pre><code>package input;
  
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  
  public class MouseInput extends MouseAdapter
  {
  public MouseInput()
  {
  
  }
  
  @Override
  public void mouseClicked(MouseEvent e)
  {
      System.out.println("Hello");
  }
  
  @Override
  public void mousePressed(MouseEvent e)
  {
      System.out.println("Mouse Pressed");
  }
  
  @Override
  public void mouseReleased(MouseEvent e)
  {
      System.out.println("Mouse Released");
  }
  }
  </code></pre>
  
  <p>Also here is my display class where I create a JFrame and I add my canvas, Key Listener and Mouse Listener to it. </p>
  
  <pre><code>package display;
  
  import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Dimension;
  
  import javax.swing.JFrame;
  
  import input.KeyInput;
  import input.MouseInput;
  
  
  public class Display
  {
  public static JFrame frame;
  private Canvas canvas;
  
  private String title;
  private int width;
  private int height;
  
  private static double currtime = System.nanoTime();
  private static double lasttime = 0;
  private static double delta = 0;
  private static float fps = 30;
  private static int tick = 0;
  
  public Display(String title, int width, int height)
  {
      this.title = title;
      this.width = width;
      this.height = height;
  
      createDisplay();
  }
  
  private void createDisplay()
  {
      frame = new JFrame(title);
      frame.setSize(width, height);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      frame.setFocusable(true);
  
      canvas = new Canvas();
      canvas.setPreferredSize(new Dimension(width, height));
      canvas.setMinimumSize(new Dimension(width, height));
      canvas.setMaximumSize(new Dimension(width, height));
      canvas.setBackground(Color.WHITE); 
  
      frame.add(canvas); 
      frame.addKeyListener(new KeyInput()); // Add the keyListener
      frame.addMouseListener(new MouseInput()); // Add the mouseListener
      frame.pack();
      System.out.println(frame.getMouseListeners().length + " mouse listener found"); // This line outputs 1 mouse listener found
      System.out.println(frame.getKeyListeners().length + " key listener found"); // This line outputs 1 key listener found
  
  }
  
  public void update() // Called every frame
  {
      frame.requestFocus();
      tick++;
      lasttime = currtime;
      currtime = System.nanoTime();
      delta = (currtime - lasttime) / 1000000000;
      fps = (float) (1 / delta);
      if (tick / getFPS() &gt;= 2)
      {
          tick = 0;
          System.out.println("FPS = " + Math.round(getFPS()));
          try
          {
              System.out.println("Mouse Position = " + frame.getMousePosition().getX()
                      + ", " + frame.getMousePosition().getY());
          }
          catch (Exception e)
          {
              System.out.println("Mouse out of screen. Could not get mouse position (NullPointerException)");
          }
      }
  }
  
  public int getWidth()
  {
      return width;
  }
  
  public int getHeight()
  {
      return height;
  }
  
  public Canvas getCanvas()
  {
      return canvas;
  }
  
  public JFrame getFrame()
  {
      return frame;
  }
  
  public static float getFPS()
  {
      return fps;
  }
  }
  </code></pre>
  
  <p>I looked through this forum for answer but every answer given did not fix it.</p>
  
  <p>Tell me if you need any more information to help you solve the problem and thanks in advance for helping :D</p>