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 thefocusGained
andfocusLost
methods. - Create a
KeyListener
and override thekeyPressed
,keyReleased
andkeyTyped
methods to monitor keyboard activity. - Create a
MouseListener
and overridemouseClicked
,mouseEntered
,mouseExited
,mousePressed
,mouseReleased
to monitor mouse activity. - Create a
MouseMotionListener
and overridemouseDragged
,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", "mouseMoved"}; 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.getContentPane().add(applet); jFrame.setSize(width, height); applet.init(); applet.start(); jFrame.setVisible(true); } class MyButton extends JButton { void report(String str, String output) { ((JTextField) hashmap.get(str)).setText(output); } FocusListener focusListener = new FocusListener() { @Override public void focusGained(FocusEvent event) { report("focusGained", event.paramString()); } @Override public void focusLost(FocusEvent event) { report("focusLost", event.paramString()); } }; KeyListener keyListener = new KeyListener() { @Override public void keyPressed(KeyEvent event) { report("keyPressed", event.paramString()); } @Override public void keyReleased(KeyEvent event) { report("keyReleased", event.paramString()); } @Override public void keyTyped(KeyEvent event) { report("keyTyped", event.paramString()); } }; MouseListener mouseListener = new MouseListener() { @Override public void mouseClicked(MouseEvent event) { report("mouseClicked", event.paramString()); } @Override public void mouseEntered(MouseEvent event) { report("mouseEntered", event.paramString()); } @Override public void mouseExited(MouseEvent event ){ report("mouseExited", event.paramString()); } @Override public void mousePressed(MouseEvent event ){ report("mousePressed", event.paramString()); } @Override public void mouseReleased(MouseEvent event) { report("mouseReleased", event.paramString()); } }; MouseMotionListener mouseMotionListener = new MouseMotionListener() { @Override public void mouseDragged(MouseEvent event) { report("mouseDragged", event.paramString()); } @Override public void mouseMoved(MouseEvent event) { report("mouseMoved", event.paramString()); } }; public MyButton(Color color, String label) { super(label); setBackground(color); addFocusListener(focusListener); addKeyListener(keyListener); addMouseListener(mouseListener); addMouseMotionListener(mouseMotionListener); } } @Override 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(); jTextField.setEditable(false); 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.