ZeePedia

JAVA: More Examples of Handling Events

<< JAVA: Event Handling
JAVA: Problem in Last Code Example >>
img
Web Design & Development ­ CS506
VU
Lesson 12
More Examples of Handling Events
Handling Mouse Event
Mouse events can be trapped for any GUI component that inherits from Component class. For example,
JPanel, JFrame & JButton etc.
To handle Mouse events, two types of listener interfaces are available.
­
MouseMotionListener
­
MouseListener
The class that wants to handle mouse event needs to implement the corresponding interface and
needs to provide the definition of all the methods in that interface.
MouseMotionListener interface
­
Used for processing mouse motion events
­
Mouse motion event is generated when mouse is moved or dragged
MouseMotionListener interfaces is defined in JDK as follows
public interface MouseMotionListener {
public void mouseDragged (MouseEvent me);
public void mouseMoved (MouseEvent me);
}
MouseListener interface
­
Used for processing "interesting" mouse events like when mouse is:
Pressed
Released
Clicked (pressed & released without moving the cursor)
Enter (mouse cursor enters the bounds of component)
Exit (mouse cursor leaves the bounds of component)
MouseListener interfaces is defined in JDK as follows
public interface MouseListener {
public void mousePressed (MouseEvent me);
public void mouseClicked (MouseEvent me);
public void mouseReleased (MouseEvent me);
public void mouseEntered (MouseEvent me);
public void mouseExited (MouseEvent me);
}
Example Code: Handling Mouse Events
Example to show Mouse Event Handling .Every time mouse is moved, the coordinates for a new
place is shown in a label.
99
img
Web Design & Development ­ CS506
VU
1. import java.awt.*;
2. import javax.swing.*;
3. import java.awt.event.*;
4. public class EventsEx implements MouseMotionListener{
5. JFrame frame;
6. JLabel coordinates;
7. // setting layout
8. public void initGUI ( ) {
9.
// creating event generator
10.
frame = new JFrame();
11.
Container cont = frame.getContentPane();
12.
cont.setLayout(new BorderLayout( ) );
13.
coordinates = new JLabel ();
14.
cont.add(coordinates, BorderLayout.NORTH);
15.
// registring mouse event handler with generator
16.
frame.addMouseMotionListener(this);
17.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18.
frame.setSize(350, 350);
19.
frame.setVisible(true);
20.
} // end initGUI method
21.
//default constructor
22.
public EventsEx ( ) {
23.
initGUI();
24.
}
// MouseMotionListener event hadler handling dragging
25.
public void mouseDragged (MouseEvent me) {
26.
int x = me.getX();
27.
int y = me.getY();
28.
coordinates.setText("Dragged at [" + x + "," + y + "]");
29.
}
// MouseMotionListener event handler handling motion
30.
public void mouseMoved (MouseEvent me) {
31.
int x = me.getX();
32.
int y = me.getY();
33.
coordinates.setText("Moved at [" + x + "," + y + "]");
100
img
Web Design & Development ­ CS506
VU
34.
}
35.
public static void main(String args[]) {
36.
EventsEx ex = new EventsEx();
37.
}
38.
} // end class
Another Example: Handling Window Events
Task
We want to handle Window Exit event only
Why?
When window is closed, control should return back to command prompt.
But we have already achieved this functionality through following line of code
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
But, what if we want to display some message (Good Bye) before exiting?
To handle window events, we need to implement "WindowListner" interface.
"WindowListner" interface contains 7 methods We require only one i.e.
windowClosing
But, We have to provide definitions of all methods to make our class a concrete class
WindowListener interface is defined in the JDK as follows
public interface WindowListener {
101
img
Web Design & Development ­ CS506
VU
public void windowActivated(WindowEvent we);
public void windowClosed(WindowEvent we);
public void windowClosing(WindowEvent we);
public void windowDeactivated(WindowEvent we);
public void windowDeiconified(WindowEvent we);
public void windowIconified(WindowEvent we);
public void windowOpened(WindowEvent we);
}
public void windowClosing(WindowEvent we is our required method
Example Code: WindowExitHandler
This example code is modification of the last code example i.e. EventsEx.java
1.
import java.awt.*;
2.
import javax.swing.*;
3.
import java.awt.event.*;
4.
public class EventsEx implements MouseMotionListener ,
WindowListener {
5.
JFrame frame;
6.
JLabel coordinates;
// setting layout
7.
public void initGUI ( ) {
// creating event generator
8.
frame = new JFrame();
9.
Container cont = frame.getContentPane();
10.
cont.setLayout(new BorderLayout( ) );
11.
coordinates = new JLabel ();
12.
cont.add(coordinates, BorderLayout.NORTH);
// registring mouse event handler with generator
13.
frame.addMouseMotionListener(this);
// registering window handler with generator
14.
frame.addWindowListener(this);
15.
frame.setSize(350, 350);
16.
frame.setVisible(true);
17.
} // end initGUI method
//default constructor
18.
public EventsEx ( ) {
19.
initGUI();
20.
}
102
img
Web Design & Development ­ CS506
VU
// MouseMotionListener event hadler handling dragging
21.
public void mouseDragged (MouseEvent me) {
22.
int x = me.getX();
23.
int y = me.getY();
24.
coordinates.setText("Dragged at [" + x + "," + y + "]");
25.
}
// MouseMotionListener event handler handling motion
26.
public void mouseMoved (MouseEvent me) {
27.
int x = me.getX();
28.
int y = me.getY();
29.
30.
coordinates.setText("Moved at [" + x + "," + y + "]");
31.
}
// window listener event handler
32.
public void windowActivated (WindowEvent we) { }
33.
public void windowClosed (WindowEvent we) { }
34.
public void windowClosing (WindowEvent we) {
35.
JOptionPane.showMessageDialog(null, "Good Bye");
36.
System.exit(0);
37.
}
38.
public void windowDeactivated (WindowEvent we) { }
39.
public void windowDeiconified (WindowEvent we) { }
40.
public void windowIconified (WindowEvent we) { }
41.
public void windowOpened (WindowEvent we) { }
42.
public static void main(String args[]) {
43.
EventsEx ex = new EventsEx();
44.
}
45.
} // end class
103