Home » Desktop Java » awt » event » KeyListener example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

KeyListener example

In this example we are going to see how you can use a KeyListener in Java, in order to monitor key events that take place in your applications. This is particularly useful when you want to add some key functionality to your app and it’s very important you want to monitor that has a very intensive keyboard activity.

Basically, to use a KeyListener in Java you have to:

  • Create a new KeyListener instance.
  • Override the methods that correspond to the key events you want to monitor e.gkeyPressedkeyReleasedkeyTyped.
  • Create a JTextField component
  • Use it’s addKeyListener method to add to it the KeyListener you’ve created.
  • Create a MouseAdapter instance.
  • Override mousePressed method to monitor the mouse button activities.

Let’s take a closer look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class KeyTest {

    public static void main(String args[]) {

  JFrame jfr = new JFrame("Key Text Sample");

  KeyTextComponent keyTtx = new KeyTextComponent();

  final JTextField jtf = new JTextField();

  ActionListener actListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent actionEvent) {

    String keyText = actionEvent.getActionCommand();

    jtf.setText(keyText);

}

  };

  keyTtx.addActionListener(actListener);

  Container cp = jfr.getContentPane();

  cp.add(keyTtx, BorderLayout.CENTER);

  cp.add(jtf, BorderLayout.SOUTH);

  jfr.setSize(200, 200);

  jfr.setVisible(true);
    }
}
class KeyTextComponent extends Canvas {

    private ActionListener listnerList = null;

    public KeyTextComponent() {

  setBackground(Color.BLACK);

  KeyListener keyListener = new KeyAdapter() {

@Override

public void keyPressed(KeyEvent keyEvent) {

    if (listnerList != null) {

  int key = keyEvent.getKeyCode();

  String keystr = KeyEvent.getKeyText(key);

  ActionEvent aEvent = new ActionEvent(this,

    ActionEvent.ACTION_PERFORMED, keystr);

  listnerList.actionPerformed(aEvent);

    }

}

  };

  MouseListener mListner = new MouseAdapter() {

@Override

public void mousePressed(MouseEvent mEvent) {

    requestFocus();

}

  };

  addKeyListener(keyListener);

  addMouseListener(mListner);
    }

    public void addActionListener(ActionListener actionListn) {

  listnerList = AWTEventMulticaster.add(listnerList,

    actionListn);
    }

    public void removeActionListener(ActionListener actionListn) {

  listnerList = AWTEventMulticaster.remove(listnerList,

    actionListn);
    }

    @Override
    public boolean isFocusTraversable() {

  return true;
    }
}

 
This was an example on how to work with the KeyListener component.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of