How to implement KeyListener in Java

In our previous post, we learned how to use ActionListener and MouseListener. Today we will be discussing KeyListener. KeyListener in Java handles all events pertaining to any action with regards to keyboard. A method will be called whenever the user typed, pressed, or released a key in the keyboard.

Implementing KeyListener in Java

package com.javapointers.javase;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class KeyListenerTest implements KeyListener, ActionListener {

    JFrame frame;
    JTextField tf;
    JLabel lbl;
    JButton btn;

    public KeyListenerTest() {
        frame = new JFrame();
        lbl = new JLabel();
        tf = new JTextField(15);
        tf.addKeyListener(this);
        btn = new JButton("Clear");
        btn.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(tf);
        panel.add(btn);

        frame.setLayout(new BorderLayout());
        frame.add(lbl, BorderLayout.NORTH);
        frame.add(panel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent ke) {
        lbl.setText("You have typed "+ke.getKeyChar());
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        lbl.setText("You have pressed "+ke.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        lbl.setText("You have released "+ke.getKeyChar());
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        tf.setText("");
    }

    public static void main(String args[]) {
        new KeyListenerTest();
    } 
}

The output will be like this:

Untitled

In this example, we have created a class that has a JTextfield and a JLabel. The JTextfield has been added to the components that are being listened to key events. Whenever the user types in the text field, a method is always being called that changes the text of the JLabel.

  • implements KeyListener

    add the keylistener interface so that we can use them to notify key events.

  • tf.addKeyListener(this);

    add textfield(tf) to the components being listened to key events.

  • public void keyTyped(KeyEvent ke) {         lbl.setText("You have typed "+ke.getKeyChar());     }

    this method is called whenever the user types in the keyboard.

  • public void keyPressed(KeyEvent ke) {         lbl.setText("You have pressed "+ke.getKeyChar());     }

    this method is called whenever the user pressed a key in the keyboard.

  • public void keyReleased(KeyEvent ke) {         lbl.setText("You have released "+ke.getKeyChar());     }

    this method is called whenever the user released the key from being pressed in the keyboard.

  • ke.getKeyChar()

    returns the key that has been pressed, released, or type in that causes the event.

In this post, we learned about Key Listener in Java and how to implement it. The next post will be about Java Look and Feel to change the look of your GUI.

Share this tutorial!