• How to implement Key Listener in Java

    In our previous post, we learned how to use Action Listener and Mouse Listener. Today we will be discussing about Key Listener. Key Listener 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 Key Listener 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 listen to key events. Whenever the user type in the textfield, 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 in key events.

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

      this method is called whenever the user type 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. Next post will be about Java Look and Feel to change the look of your GUI.

    Related Post