event
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
enableEventsto enable the events you want for the window - Override
processMouseEventmehtod to monitor moouse events - Override
processMouseMotionEventmethod to monitor the movement of the mouse pointer in the window - Override
processKeyEventmethod to monitor key events - Override
processFocusEventandprocessComponentEventmethods to monitor the focus changes and the state of your window - Use a
JFrameto create the new window and add to it theJPanelyou’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.


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