Home » Desktop Java » awt » event » Display all events occurring in a window

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.

Display all events occurring in a window

In this example we shall show you how to display all events occurring in a windows. This is very useful when you want to have full control o events that occur in a Java applications that has many windows to handle. It is a very simple ways to manage the activity on your windows.

To display all the events that occur in a window :

  • Create class that extends JPanel
  • Use enableEvents to enable the events you want for the window
  • Override processMouseEvent mehtod to monitor moouse events
  • Override processMouseMotionEvent method to monitor the movement of the mouse pointer in the window
  • Override processKeyEvent method to monitor key events
  • Override processFocusEvent and processComponentEvent methods to monitor the focus changes and the state of your window
  • Use a JFrame to create the new window and add to it the JPanel you’ve created

Let’s take a closer look at the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class OccuringEventDisplay extends JPanel {

    public OccuringEventDisplay() {

  this.enableEvents(AWTEvent.MOUSE_EVENT_MASK

    | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK

    | AWTEvent.FOCUS_EVENT_MASK | AWTEvent.COMPONENT_EVENT_MASK

    | AWTEvent.WINDOW_EVENT_MASK);

  this.setPreferredSize(new Dimension(500, 400));
    }

    @Override
    public void processMouseEvent(MouseEvent event) {

  String type = null;

  switch (event.getID()) {

case MouseEvent.MOUSE_PRESSED:

    type = "MOUSE_PRESSED";

    break;

case MouseEvent.MOUSE_RELEASED:

    type = "MOUSE_RELEASED";

    break;

case MouseEvent.MOUSE_CLICKED:

    type = "MOUSE_CLICKED";

    break;

case MouseEvent.MOUSE_ENTERED:

    type = "MOUSE_ENTERED";

    break;

case MouseEvent.MOUSE_EXITED:

    type = "MOUSE_EXITED";

    break;

  }

  diplay(mousemods(event) + type + ": [" + event.getX() + "," + event.getY() + "] "

    + "num clicks = " + event.getClickCount()

    + (event.isPopupTrigger() ? "; is popup trigger" : ""));

  if (event.getID() == MouseEvent.MOUSE_ENTERED) {

requestFocus();

  }
    }

    @Override
    public void processMouseMotionEvent(MouseEvent event) {

  String str = null;

  switch (event.getID()) {

case MouseEvent.MOUSE_MOVED:

    str = "MOUSE_MOVED";

    break;

case MouseEvent.MOUSE_DRAGGED:

    str = "MOUSE_DRAGGED";

    break;

  }

  diplay(mousemods(event) + str + ": [" + event.getX() + "," + event.getY() + "] "

    + "num clicks = " + event.getClickCount()

    + (event.isPopupTrigger() ? "; is popup trigger" : ""));
    }

    protected String mousemods(MouseEvent event) {

  int modifs = event.getModifiers();

  String str = "";

  if (event.isShiftDown()) {

str += "Shift ";

  }

  if (event.isControlDown()) {

str += "Ctrl ";

  }

  if ((modifs & InputEvent.BUTTON1_MASK) != 0) {

str += "Button 1 ";

  }

  if ((modifs & InputEvent.BUTTON2_MASK) != 0) {

str += "Button 2 ";

  }

  if ((modifs & InputEvent.BUTTON3_MASK) != 0) {

str += "Button 3 ";

  }

  return str;
    }

    @Override
    public void processKeyEvent(KeyEvent event) {

  String eventtype, mods, code, ch;

  switch (event.getID()) {

case KeyEvent.KEY_PRESSED:

    eventtype = "KEY_PRESSED";

    break;

case KeyEvent.KEY_RELEASED:

    eventtype = "KEY_RELEASED";

    break;

case KeyEvent.KEY_TYPED:

    eventtype = "KEY_TYPED";

    break;

default:

    eventtype = "UNKNOWN";

  }

  mods = KeyEvent.getKeyModifiersText(event.getModifiers());

  if (event.getID() == KeyEvent.KEY_TYPED) {

code = "";

  } else {

code = "Code=" + KeyEvent.getKeyText(event.getKeyCode()) + " ("

  + event.getKeyCode() + ")";

  }

  // Get string and numeric versions of the Unicode character, if any.

  if (event.isActionKey()) {

ch = "";

  } else {

ch = "Character=" + event.getKeyChar() + " (Unicode="

  + ((int) event.getKeyChar()) + ")";

  }

  // Display it all.

  diplay(eventtype + ": " + mods + " " + code + " " + ch);
    }

    @Override
    public void processFocusEvent(FocusEvent event) {

  if (event.getID() == FocusEvent.FOCUS_GAINED) {

diplay("FOCUS_GAINED" + (event.isTemporary() ? " (temporary)" : ""));

  } else {

diplay("FOCUS_LOST" + (event.isTemporary() ? " (temporary)" : ""));

  }
    }

    @Override
    public void processComponentEvent(ComponentEvent event) {

  switch (event.getID()) {

case ComponentEvent.COMPONENT_MOVED:

    diplay("COMPONENT_MOVED");

    break;

case ComponentEvent.COMPONENT_RESIZED:

    diplay("COMPONENT_RESIZED");

    break;

case ComponentEvent.COMPONENT_HIDDEN:

    diplay("COMPONENT_HIDDEN");

    break;

case ComponentEvent.COMPONENT_SHOWN:

    diplay("COMPONENT_SHOWN");

    break;

  }
    }

    public void processWindowEvent(WindowEvent event) {

  switch (event.getID()) {

case WindowEvent.WINDOW_OPENED:

    diplay("WINDOW_OPENED");

    break;

case WindowEvent.WINDOW_CLOSED:

    diplay("WINDOW_CLOSED");

    break;

case WindowEvent.WINDOW_CLOSING:

    diplay("WINDOW_CLOSING");

    break;

case WindowEvent.WINDOW_ICONIFIED:

    diplay("WINDOW_ICONIFIED");

    break;

case WindowEvent.WINDOW_DEICONIFIED:

    diplay("WINDOW_DEICONIFIED");

    break;

case WindowEvent.WINDOW_ACTIVATED:

    diplay("WINDOW_ACTIVATED");

    break;

case WindowEvent.WINDOW_DEACTIVATED:

    diplay("WINDOW_DEACTIVATED");

    break;

  }

  if (event.getID() == WindowEvent.WINDOW_CLOSING) {

diplay("WINDOW_CLOSING event received.");

diplay("Application will exit in 5 seconds");

update(this.getGraphics());

try {

    Thread.sleep(5000);

} catch (InterruptedException ex) {

    ;

}

// Exit now

System.exit(0);

  }
    }

    protected Vector lines = new Vector();

    protected void diplay(String str) {

  if (lines.size() == 20) {

lines.removeElementAt(0);

  }

  lines.addElement(str);

  repaint();
    }

    @Override
    public void paintComponent(Graphics graphic) {

  for (int it = 0; it < lines.size(); it++) {

graphic.drawString((String) lines.elementAt(it), 20, it * 16 + 50);

  }
    }

    @Override
    public boolean isOpaque() {

  return false;
    }

    private static void showUI() {

  JFrame frame = new JFrame();

  frame.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

    System.exit(0);

}

  });

  frame.getContentPane().add(new OccuringEventDisplay(), BorderLayout.CENTER);

  frame.setSize(600, 400);

  frame.setVisible(true);
    }

    public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

    showUI(); 

}

  });
    }

}

 
This was an example on how to Display all events occurring in a window in Java.

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

 

Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ra58
ra58
2 years ago

good work , i want an idea for keeping trace of all events even outside the window of the application , can you help me please ?!