Display all the occurring events

In this tutorial we are going to create a simple application that displays all the occurring events that might take place in a Java Desktop Application. You might find this particularly useful when developing your own applications and you want to fully control the events that occur and give proper feedback to the user. You can also customize the behavior of your application according to the occurrence of a specific event. For example you might want your application to behave differently according to witch button the user presses.

In short in order to display and handle all the occurring events, one should follows these steps:

  • Create a HashMap that will hold all the events that you might want to monitor.
  • Create a String array with all the events you want to monitor.
  • Create a FocusListener and override the focusGained and focusLost methods.
  • Create a KeyListener and override the keyPressed, keyReleased and keyTyped methods to monitor keyboard activity.
  • Create a MouseListener and override mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased to monitor mouse activity.
  • Create a MouseMotionListener and override mouseDragged, mouseMoved method to monitor mouse movements.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JApplet {

    private HashMap hashmap = new HashMap();
    private String[] eventlist = {"focusGained", "focusLost", "keyPressed", "keyReleased", "keyTyped", "mouseClicked", "mouseEntered",

  "mouseExited", "mousePressed", "mouseReleased", "mouseDragged",


    private MyButton button1 = new MyButton(Color.cyan, "Button 1");

    private MyButton button2 = new MyButton(Color.ORANGE, "Button 2");

    public static void main(String[] args) {

  run(new Main(), 800, 600);

    public static void run(JApplet applet, int width, int height) {

  JFrame jFrame = new JFrame();



  jFrame.setSize(width, height);




    class MyButton extends JButton {

  void report(String str, String output) {

((JTextField) hashmap.get(str)).setText(output);


  FocusListener focusListener = new FocusListener() {


public void focusGained(FocusEvent event) {

    report("focusGained", event.paramString());



public void focusLost(FocusEvent event) {

    report("focusLost", event.paramString());



  KeyListener keyListener = new KeyListener() {


public void keyPressed(KeyEvent event) {

    report("keyPressed", event.paramString());



public void keyReleased(KeyEvent event) {

    report("keyReleased", event.paramString());



public void keyTyped(KeyEvent event) {

    report("keyTyped", event.paramString());



  MouseListener mouseListener = new MouseListener() {


public void mouseClicked(MouseEvent event) {

    report("mouseClicked", event.paramString());



public void mouseEntered(MouseEvent event) {

    report("mouseEntered", event.paramString());



public void mouseExited(MouseEvent event ){

    report("mouseExited", event.paramString());



public void mousePressed(MouseEvent event ){

    report("mousePressed", event.paramString());



public void mouseReleased(MouseEvent event) {

    report("mouseReleased", event.paramString());



  MouseMotionListener mouseMotionListener = new MouseMotionListener() {


public void mouseDragged(MouseEvent event) {

    report("mouseDragged", event.paramString());



public void mouseMoved(MouseEvent event) {

    report("mouseMoved", event.paramString());



  public MyButton(Color color, String label) {








    public void init() {

  Container container = getContentPane();

  container.setLayout(new GridLayout(eventlist.length + 1, 2));

  for (int c = 0; c < eventlist.length; c++) {

JTextField jTextField = new JTextField();


container.add(new JLabel(eventlist

, JLabel.RIGHT)); container.add(jTextField); hashmap.put(eventlist

, jTextField); } container.add(button1); container.add(button2); } }

This was an example on how to display all the occurring events in a Java Desktop application.

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.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button