event

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:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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.

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


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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button