Java Reference
In-Depth Information
public class MyMouseListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "You clicked the mouse!");
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
}
This produces the mesmerizing application shown in Figure 12-5.
figure 12-5  
Note that nothing prevents you from implementing similar listener systems in your own
applications. All you need is an interface and subjects that know how to register and notify them.
Note that—as you've seen in the chapter dealing with GUI applications—you can also create one‐
shot throwaway listeners, like so:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GUIListenerExample {
public static void main(String[] args) {
// Create our subject
JFrame frame = new JFrame();
Search WWH ::




Custom Search