Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseListener/KeyListener not working (JPanel)

I'm doing a little project that involves the mouse and key listeners in JPanel. Unfortunately, none of the methods are called when I use the mouse/keyboard. I have worked with JPanels/JFrame/JApplet and JComponents before. The code snippets are shown below:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Scanner;

public class Hello extends JPanel implements KeyListener, MouseListener{
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    public Hello(){
        addKeyListener(this);
        addMouseListener(this);
    }
    public static void main(String [] args){
        Hello play = new Hello();
        play.setPanel();
    }
    public void setPanel(){
        panel.setLayout(null);
        frame.add(panel);
        frame.setLayout(null);
        panel.setBounds(0,0,100,100);
        frame.setVisible(true);
        panel.setVisible(true);
        panel.setFocusable(true);
        frame.setSize(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void keyTyped(KeyEvent evt){
        System.out.println("keytyped");
    }
    public void keyPressed(KeyEvent evt){
        System.out.print("keypressed");
    }
    public void keyReleased(KeyEvent evt){
        System.out.println("keyreleased");
    }
    public void mousePressed(MouseEvent evt){
        System.out.println("mousepressed");
    }
    public void mouseReleased(MouseEvent evt){
        System.out.println("mousereleased");
    }
    public void mouseClicked(MouseEvent evt){
        System.out.println("mouseclicked");
    }
    public void mouseEntered(MouseEvent evt){
        System.out.println("mousenentered");
    }
    public void mouseExited(MouseEvent evt){
        System.out.println("mouseexited");
    }
}

Off topic: I keep getting the error Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. I have no idea how to fix it. Sometimes I put everything in code and it still won't submit.

like image 645
GoldenLyfe Avatar asked Dec 29 '12 20:12

GoldenLyfe


2 Answers

Have a look at Java KeyListener for JFrame is being unresponsive?.

You need to register your KeyListener and MouseListener for every JComponent you want to listen to:

public Hello() {
    addKeyListener(this);
    addMouseListener(this);
    panel.addKeyListener(this);
    panel.addMouseListener(this);
    frame.addKeyListener(this);
    frame.addMouseListener(this);
}

Edit:
Key and mouse events are only fired from the JComponent which has focus at the time. Because of this there seems to be a consensus that KeyBindings may be favorable to KeyListeners. The two have their applications, however, and so there is no hard and fast rule here. Have a read of 'How to Write a Key Listener' and 'How to Write a Key Binding' and you'll get the gist.

like image 54
Sean Connolly Avatar answered Sep 20 '22 18:09

Sean Connolly


Better to avoid using KeyListeners with JPanel, use KeyBindings instead. JPanel cannot gain focus so cannot interact with KeyEvents. Using KeyBindings, you can map an Action to a KeyStroke even when a component doesn't have focus.

like image 23
Reimeus Avatar answered Sep 19 '22 18:09

Reimeus