Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to simply implement a KeyListener?

public class MyPanel extends JPanel implements KeyListener {
    private char c = 'e';
    public MyPanel() {
        this.setPreferredSize(new Dimension(500,500));
        addKeyListener(this);
    }
    public void paintComponent(Graphics g) {
        super.repaint();
        g.drawString("the key that pressed is" + c, 250,250);
    }

    public void keyPressed(KeyEvent e) {
        c=e.getKeyChar();
        repaint();

    }

    public void keyReleased(KeyEvent e) {
    }


    public void keyTyped(KeyEvent e) {
        c=e.getKeyChar();
        repaint();
    }

    public static void main(String[] s) {
        JFrame f=new JFrame();
        f.getContentPane().add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }
}

I tried reading this yet didnt mange to understand how to simply implement a KeyListener. so what do i need to change for this to work?

like image 580
Ofek Ron Avatar asked Feb 17 '12 19:02

Ofek Ron


People also ask

What is the purpose of KeyListener interface?

Interface KeyListener The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

Which of the following interface is implemented for KeyEvent?

Java Prime Pack The class which processes the KeyEvent should implement this interface. The object of that class must be registered with a component. The object can be registered using the addKeyListener() method.

How do you write a key event in Java?

*/ public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: "); } /** Handle the key-pressed event from the text field. */ public void keyPressed(KeyEvent e) { displayInfo(e, "KEY PRESSED: "); } /** Handle the key-released event from the text field.


1 Answers

Here are the reasons why it doesn't work:

  1. The JPanel does not have the keyboard focus. (The frame has it.) You probably want to requestFocus when the panel is added to the screen.

  2. You need to call repaint when the graphic should change.

  3. You mustn't call repaint in the paintComponent method.

  4. You need to clear the drawing area before drawing the string again (otherwise all characters will end up on top of each other).

Here's a complete working example:

class MyPanel extends JPanel implements KeyListener {
    private char c = 'e';

    public MyPanel() {
        this.setPreferredSize(new Dimension(500, 500));
        addKeyListener(this);
    }

    public void addNotify() {
        super.addNotify();
        requestFocus();
    }

    public void paintComponent(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        g.drawString("the key that pressed is " + c, 250, 250);
    }

    public void keyPressed(KeyEvent e) { }
    public void keyReleased(KeyEvent e) { }
    public void keyTyped(KeyEvent e) {
        c = e.getKeyChar();
        repaint();
    }

    public static void main(String[] s) {
        JFrame f = new JFrame();
        f.getContentPane().add(new MyPanel());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

Oh, and you may want to add f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to make the application terminate when you close the window. :-)

like image 169
aioobe Avatar answered Sep 21 '22 12:09

aioobe